home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 317 / asmsrc / m68k.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-20  |  66.8 KB  |  2,978 lines

  1. /* m68k.c  All the m68020 specific stuff in one convenient
  2.    huge slow-to-compile easy to find file. */
  3.  
  4. /* Copyright (C) 1987 Free Software Foundation, Inc.
  5.  
  6. This file is part of Gas, the GNU Assembler.
  7.  
  8. The GNU assembler is distributed in the hope that it will be
  9. useful, but WITHOUT ANY WARRANTY.  No author or distributor
  10. accepts responsibility to anyone for the consequences of using it
  11. or for whether it serves any particular purpose or works at all,
  12. unless he says so in writing.  Refer to the GNU Assembler General
  13. Public License for full details.
  14.  
  15. Everyone is granted permission to copy, modify and redistribute
  16. the GNU Assembler, but only under the conditions described in the
  17. GNU Assembler General Public License.  A copy of this license is
  18. supposed to have been given to you along with the GNU Assembler
  19. so you can know your rights and responsibilities.  It should be
  20. in a file named COPYING.  Among other things, the copyright
  21. notice and this notice must be preserved on all copies.  */
  22.  
  23. #include <stdio.h>
  24. #include <ctype.h>
  25.  
  26. #include "m68k-opcode.h"
  27. #include "as.h"
  28. #include "obstack.h"
  29. #include "frags.h"
  30. #include "struc-symbol.h"
  31. #include "flonum.h"
  32. #include "expr.h"
  33. #include "hash.h"
  34. #include "md.h"
  35. #include "m68k.h"
  36.  
  37. #ifdef M_SUN
  38. /* This variable contains the value to write out at the beginning of
  39.    the a.out file.  The 2<<16 means that this is a 68020 file instead
  40.    of an old-style 68000 file */
  41.  
  42. long omagic = 2<<16|OMAGIC;    /* Magic byte for header file */
  43. #else
  44. long omagic = OMAGIC;
  45. #endif
  46.  
  47.  
  48. /* This array holds the chars that always start a comment.  If the
  49.    pre-processor is disabled, these aren't very useful */
  50. char comment_chars[] = "|";
  51.  
  52. /* This array holds the chars that only start a comment at the beginning of
  53.    a line.  If the line seems to have the form '# 123 filename'
  54.    .line and .file directives will appear in the pre-processed output */
  55. /* Note that input_file.c hand checks for '#' at the beginning of the
  56.    first line of the input file.  This is because the compiler outputs
  57.    #NO_APP at the beginning of its output. */
  58. /* Also note that '/*' will always start a comment */
  59. char line_comment_chars[] = "#";
  60.  
  61. /* Chars that can be used to separate mant from exp in floating point nums */
  62. char EXP_CHARS[] = "eE";
  63.  
  64. /* Chars that mean this number is a floating point constant */
  65. /* As in 0f12.456 */
  66. /* or    0d1.2345e12 */
  67.  
  68. char FLT_CHARS[] = "rRsSfFdDxXpP";
  69.  
  70. /* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
  71.    changed in read.c .  Ideally it shouldn't have to know about it at all,
  72.    but nothing is ideal around here.
  73.  */
  74.  
  75. void fix_new();
  76.  
  77. /* Its an arbitrary name:  This means I don't approve of it */
  78. /* See flames below */
  79. struct obstack robyn;
  80.  
  81. #define TAB(x,y)    (((x)<<2)+(y))
  82. #define BYTE        0
  83. #define SHORT        1
  84. #define LONG        2
  85. #define SZ_UNDEF    3
  86.  
  87. #define BRANCH        1
  88. #define FBRANCH        2
  89. #define PCREL        3
  90.  
  91. /* This table desribes how you change sizes for the various types of variable
  92.    size expressions.  This version only supports two kinds. */
  93.  
  94. /* The fields are:
  95. /*    How far Forward this mode will reach:
  96.     How far Backward this mode will reach:
  97.     How many bytes this mode will add to the size of the frag
  98.     Which mode to go to if the offset won't fit in this one
  99.  */
  100. relax_typeS
  101. md_relax_table[] = {
  102. { 1,        1,        0,    0 },    /* First entries aren't used */
  103. { 1,        1,        0,    0 },    /* For no good reason except */
  104. { 1,        1,        0,    0 },    /* that the VAX doesn't either */
  105. { 1,        1,        0,    0 },
  106.  
  107. { (127),    (-128),        0,    TAB(BRANCH,SHORT)},
  108. { (2+32767),    (2+-32768),    2,    TAB(BRANCH,LONG) },
  109. { 0,        0,        4,    0 },
  110. { 1,        1,        0,    0 },
  111.  
  112. { 1,        1,        0,    0 },    /* FBRANCH doesn't come BYTE */
  113. { (2+32767),    (2+-32768),    2,    TAB(FBRANCH,LONG)},
  114. { 0,        0,        4,    0 },
  115. { 1,        1,        0,    0 },
  116.  
  117. { 1,        1,        0,    0 },    /* PCREL doesn't come BYTE */
  118. { (2+32767),    (2+-32768),    2,    TAB(PCREL,LONG)},
  119. { 0,        0,        4,    0 },
  120. { 1,        1,        0,    0 },
  121. };
  122.  
  123. void    s_data1(),    s_data2(),    s_even(),    s_space();
  124.  
  125. /* These are the machine dependent pseudo-ops.  These are included so
  126.    the assembler can work on the output from the SUN C compiler, which
  127.    generates these.
  128.  */
  129.  
  130. /* This table describes all the machine specific pseudo-ops the assembler
  131.    has to support.  The fields are:
  132.          pseudo-op name without dot
  133.       function to call to execute this pseudo-op
  134.       Integer arg to pass to the function
  135.  */
  136. pseudo_typeS md_pseudo_table[] = {
  137.     { "data1",    s_data1,    0    },
  138.     { "data2",    s_data2,    0    },
  139.     { "even",    s_even,        0    },
  140.     { "skip",    s_space,    0    },
  141.     { 0,        0,        0    }
  142. };
  143.  
  144.  
  145. /* #define isbyte(x)    ((x)>=-128 && (x)<=127) */
  146. /* #define isword(x)    ((x)>=-32768 && (x)<=32767) */
  147.  
  148. #define issbyte(x)    ((x)>=-128 && (x)<=127)
  149. #define isubyte(x)    ((x)>=0 && (x)<=255)
  150. #define issword(x)    ((x)>=-32768 && (x)<=32767)
  151. #define isuword(x)    ((x)>=0 && (x)<=65535)
  152.  
  153. #define isbyte(x)    ((x)>=-128 && (x)<=255)
  154. #define isword(x)    ((x)>=-32768 && (x)<=65535)
  155. #define islong(x)    (1)
  156.  
  157. char *input_line_pointer;
  158.  
  159. /* Operands we can parse:  (And associated modes)
  160.  
  161. numb:    8 bit num
  162. numw:    16 bit num
  163. numl:    32 bit num
  164. dreg:    data reg 0-7
  165. reg:    address or data register
  166. areg:    address register
  167. apc:    address register, PC, ZPC or empty string
  168. num:    16 or 32 bit num
  169. num2:    like num
  170. sz:    w or l        if omitted, l assumed
  171. scale:    1 2 4 or 8    if omitted, 1 assumed
  172.  
  173. 7.4 IMMED #num                --> NUM
  174. 0.? DREG  dreg                --> dreg
  175. 1.? AREG  areg                --> areg
  176. 2.? AINDR areg@                --> *(areg)
  177. 3.? AINC  areg@+            --> *(areg++)
  178. 4.? ADEC  areg@-            --> *(--areg)
  179. 5.? AOFF  apc@(numw)            --> *(apc+numw)    -- empty string and ZPC not allowed here
  180. 6.? AINDX apc@(num,reg:sz:scale)    --> *(apc+num+reg*scale)
  181. 6.? AINDX apc@(reg:sz:scale)        --> same, with num=0
  182. 6.? APODX apc@(num)@(num2,reg:sz:scale)    --> *(*(apc+num)+num2+reg*scale)
  183. 6.? APODX apc@(num)@(reg:sz:scale)    --> same, with num2=0
  184. 6.? AMIND apc@(num)@(num2)        --> *(*(apc+num)+num2) (previous mode without an index reg)
  185. 6.? APRDX apc@(num,reg:sz:scale)@(num2)    --> *(*(apc+num+reg*scale)+num2)
  186. 6.? APRDX apc@(reg:sz:scale)@(num2)    --> same, with num=0
  187. 7.0 ABSL  num:sz            --> *(num)
  188.           num                --> *(num) (sz L assumed)
  189. *** MSCR  otherreg            --> Magic
  190. With -l option
  191. 5.? AOFF  apc@(num)            --> *(apc+num) -- empty string and ZPC not allowed here still
  192.  
  193. examples:
  194.     #foo    #0x35    #12
  195.     d2
  196.     a4
  197.     a3@
  198.     a5@+
  199.     a6@-
  200.     a2@(12)    pc@(14)
  201.     a1@(5,d2:w:1)    @(45,d6:l:4)
  202.     pc@(a2)        @(d4)
  203.     etc . . .
  204.  
  205.  
  206. #name@(numw)    -->turn into PC rel mode
  207. apc@(num8,reg:sz:scale)        --> *(apc+num8+reg*scale)
  208.  
  209. */
  210.  
  211. #define IMMED    1
  212. #define DREG    2
  213. #define AREG    3
  214. #define AINDR    4
  215. #define ADEC    5
  216. #define AINC    6
  217. #define AOFF    7
  218. #define AINDX    8
  219. #define APODX    9
  220. #define AMIND    10
  221. #define APRDX    11
  222. #define ABSL    12
  223. #define MSCR    13
  224. #define REGLST    14
  225.  
  226. #define FAIL    0
  227. #define OK    1
  228.  
  229. /* DATA and ADDR have to be contiguous, so that reg-DATA gives 0-7==data reg,
  230.    8-15==addr reg for operands that take both types */
  231. #define DATA    1        /*   1- 8 == data registers 0-7 */
  232. #define ADDR    (DATA+8)    /*   9-16 == address regs 0-7 */
  233. #define FPREG    (ADDR+8)    /*  17-24 Eight FP registers */
  234. #define COPNUM    (FPREG+8)    /*  25-32 Co-processor #1-#8 */
  235.  
  236. #define PC    (COPNUM+8)    /*  33 Program counter */
  237. #define ZPC    (PC+1)        /*  34 Hack for Program space, but 0 addressing */
  238. #define SR    (ZPC+1)        /*  35 Status Reg */
  239. #define CCR    (SR+1)        /*  36 Condition code Reg */
  240.  
  241. /* These have to be in order for the movec instruction to work. */
  242. #define USP    (CCR+1)        /*  37 User Stack Pointer */
  243. #define ISP    (USP+1)        /*  38 Interrupt stack pointer */
  244. #define SFC    (ISP+1)        /*  39 */
  245. #define DFC    (SFC+1)        /*  40 */
  246. #define CACR    (DFC+1)        /*  41 */
  247. #define VBR    (CACR+1)    /*  42 */
  248. #define CAAR    (VBR+1)        /*  43 */
  249. #define MSP    (CAAR+1)    /*  44 */
  250.  
  251. #define FPI    (MSP+1)        /* 45 */
  252. #define FPS    (FPI+1)        /* 46 */
  253. #define FPC    (FPS+1)        /* 47 */
  254.  
  255. /* Note that COPNUM==processor #1 -- COPNUM+7==#8, which stores as 000 */
  256. /* I think. . .  */
  257.  
  258. #define    SP    ADDR+7
  259.  
  260. /* JF these tables here are for speed at the expense of size */
  261. /* You can replace them with the #if 0 versions if you really
  262.    need space and don't mind it running a bit slower */
  263.  
  264. static char mklower_table[256];
  265. #define mklower(c) (mklower_table[(unsigned char)(c)])
  266. static char notend_table[256];
  267. static char alt_notend_table[256];
  268. #define notend(s) ( !(notend_table[(unsigned char)(*s)] || (*s==':' &&\
  269.  alt_notend_table[(unsigned char)(s[1])])))
  270.  
  271. #if 0
  272. #define mklower(c)    (isupper(c) ? tolower(c) : c)
  273. #endif
  274.  
  275.  
  276. struct m68k_exp {
  277.     char    *e_beg;
  278.     char    *e_end;
  279.     expressionS e_exp;
  280.     short    e_siz;        /* 0== default 1==short/byte 2==word 3==long */
  281. };
  282.  
  283. /* Internal form of an operand.  */
  284. struct m68k_op {
  285.     char    *error;        /* Couldn't parse it */
  286.     int    mode;        /* What mode this instruction is in.  */
  287.     unsigned long int    reg;        /* Base register */
  288.     struct m68k_exp *con1;
  289.     int    ireg;        /* Index register */
  290.     int    isiz;        /* 0==unspec  1==byte(?)  2==short  3==long  */
  291.     int    imul;        /* Multipy ireg by this (1,2,4,or 8) */
  292.     struct    m68k_exp *con2;
  293. };
  294.  
  295. /* internal form of a 68020 instruction */
  296. struct m68_it {
  297.     char    *error;
  298.     char    *args;        /* list of opcode info */
  299.     int    numargs;
  300.  
  301.     int    numo;        /* Number of shorts in opcode */
  302.     short    opcode[11];
  303.  
  304.     struct m68k_op operands[6];
  305.  
  306.     int    nexp;        /* number of exprs in use */
  307.     struct m68k_exp exprs[4];
  308.  
  309.     int    nfrag;        /* Number of frags we have to produce */
  310.     struct {
  311.         int fragoff;    /* Where in the current opcode[] the frag ends */
  312.         symbolS *fadd;
  313.         long int foff;
  314.         int fragty;
  315.     } fragb[4];
  316.  
  317.     int    nrel;        /* Num of reloc strucs in use */
  318.     struct    {
  319.         int    n;
  320.         symbolS    *add,
  321.             *sub;
  322.         long int off;
  323.         char    wid;
  324.         char    pcrel;
  325.     } reloc[5];        /* Five is enough??? */
  326. };
  327.  
  328. struct m68_it the_ins;        /* the instruction being assembled */
  329.  
  330.  
  331. /* Macros for adding things to the m68_it struct */
  332.  
  333. #define addword(w)    the_ins.opcode[the_ins.numo++]=(w)
  334.  
  335. /* Like addword, but goes BEFORE general operands */
  336. #define insop(w)    {int z;\
  337.  for(z=the_ins.numo;z>opcode->m_codenum;--z)\
  338.    the_ins.opcode[z]=the_ins.opcode[z-1];\
  339.  for(z=0;z<the_ins.nrel;z++)\
  340.    the_ins.reloc[z].n+=2;\
  341.  the_ins.opcode[opcode->m_codenum]=w;\
  342.  the_ins.numo++;\
  343. }
  344.  
  345.  
  346. #define add_exp(beg,end) (\
  347.     the_ins.exprs[the_ins.nexp].e_beg=beg,\
  348.     the_ins.exprs[the_ins.nexp].e_end=end,\
  349.     &the_ins.exprs[the_ins.nexp++]\
  350. )
  351.  
  352.  
  353. /* The numo+1 kludge is so we can hit the low order byte of the prev word. Blecch*/
  354. #define add_fix(width,exp,pc_rel) {\
  355.     the_ins.reloc[the_ins.nrel].n= (width=='B') ? (the_ins.numo*2-1) : \
  356.         ((width=='b') ? ((the_ins.numo-1)*2) : (the_ins.numo*2));\
  357.     the_ins.reloc[the_ins.nrel].add=adds((exp));\
  358.     the_ins.reloc[the_ins.nrel].sub=subs((exp));\
  359.     the_ins.reloc[the_ins.nrel].off=offs((exp));\
  360.     the_ins.reloc[the_ins.nrel].wid=width;\
  361.     the_ins.reloc[the_ins.nrel++].pcrel=pc_rel;\
  362. }
  363.  
  364. #define add_frag(add,off,type)  {\
  365.     the_ins.fragb[the_ins.nfrag].fragoff=the_ins.numo;\
  366.     the_ins.fragb[the_ins.nfrag].fadd=add;\
  367.     the_ins.fragb[the_ins.nfrag].foff=off;\
  368.     the_ins.fragb[the_ins.nfrag++].fragty=type;\
  369. }
  370.  
  371. #define isvar(exp)    ((exp) && (adds(exp) || subs(exp)))
  372.  
  373. #define seg(exp)    ((exp)->e_exp.X_seg)
  374. #define adds(exp)    ((exp)->e_exp.X_add_symbol)
  375. #define subs(exp)    ((exp)->e_exp.X_subtract_symbol)
  376. #define offs(exp)    ((exp)->e_exp.X_add_number)
  377.  
  378.  
  379. struct m68_incant {
  380.     char *m_operands;
  381.     unsigned long m_opcode;
  382.     short m_opnum;
  383.     short m_codenum;
  384.     struct m68_incant *m_next;
  385. };
  386.  
  387. #define getone(x)    ((((x)->m_opcode)>>16)&0xffff)
  388. #define gettwo(x)    (((x)->m_opcode)&0xffff)
  389.  
  390.  
  391. /* JF modified this to handle cases where the first part of a symbol name
  392.    looks like a register */
  393.  
  394. m68k_reg_parse(ccp)
  395. register char **ccp;
  396. {
  397.     register char c1,
  398.         c2,
  399.         c3,
  400.         c4;
  401.     register int n = 0,
  402.         ret;
  403.  
  404.     c1=mklower(ccp[0][0]);
  405.     c2=mklower(ccp[0][1]);
  406.     c3=mklower(ccp[0][2]);
  407.     c4=mklower(ccp[0][3]);
  408.  
  409.     switch(c1) {
  410.     case 'a':
  411.         if(c2>='0' && c2<='7') {
  412.             n=2;
  413.             ret=ADDR+c2-'0';
  414.         }
  415. #ifdef m68851
  416.         else if (c2 == 'c') {
  417.             n = 2;
  418.             ret = AC;
  419.         }
  420. #endif
  421.         break;
  422. #ifdef m68851
  423.     case 'b':
  424.         if (c2 == 'a') {
  425.             if (c3 == 'd') {
  426.                 if (c4 >= '0' && c4 <= '7') {
  427.                     n = 4;
  428.                     ret = BAD + c4 - '0';
  429.                 }
  430.             }
  431.             if (c3 == 'c') {
  432.                 if (c4 >= '0' && c4 <= '7') {
  433.                     n = 4;
  434.                     ret = BAC + c4 - '0';
  435.                 }
  436.             }
  437.         }
  438.         break;
  439. #endif
  440.     case 'c':
  441. #ifdef m68851
  442.         if (c2 == 'a' && c3 == 'l') {
  443.             n = 3;
  444.             ret = CAL;
  445.         } else
  446. #endif
  447.             /* This supports both CCR and CC as the ccr reg. */
  448.         if(c2=='c' && c3=='r') {
  449.             n=3;
  450.             ret = CCR;
  451.         } else if(c2=='c') {
  452.             n=2;
  453.             ret = CCR;
  454.         } else if(c2=='a' && (c3=='a' || c3=='c') && c4=='r') {
  455.             n=4;
  456.             ret = c3=='a' ? CAAR : CACR;
  457.         }
  458. #ifdef m68851
  459.         else if (c2 == 'r' && c3 == 'p') {
  460.             n = 3;
  461.             ret = (CRP);
  462.         }
  463. #endif
  464.         break;
  465.     case 'd':
  466.         if(c2>='0' && c2<='7') {
  467.             n=2;
  468.             ret = DATA+c2-'0';
  469.         } else if(c2=='f' && c3=='c') {
  470.             n=3;
  471.             ret = DFC;
  472.         }
  473. #ifdef m68851
  474.         else if (c2 == 'r' && c3 == 'p') {
  475.             n = 3;
  476.             ret = (DRP);
  477.         }
  478. #endif
  479.         break;
  480.     case 'f':
  481.         if(c2=='p') {
  482.              if(c3>='0' && c3<='7') {
  483.                 n=3;
  484.                 ret = FPREG+c3-'0';
  485.             } else if(c3=='i') {
  486.                 n=3;
  487.                 ret = FPI;
  488.             } else if(c3=='s') {
  489.                 n= (c4 == 'r' ? 4 : 3);
  490.                 ret = FPS;
  491.             } else if(c3=='c') {
  492.                 n= (c4 == 'r' ? 4 : 3);
  493.                 ret = FPC;
  494.             }
  495.         }
  496.         break;
  497.     case 'i':
  498.         if(c2=='s' && c3=='p') {
  499.             n=3;
  500.             ret = ISP;
  501.         }
  502.         break;
  503.     case 'm':
  504.         if(c2=='s' && c3=='p') {
  505.             n=3;
  506.             ret = MSP;
  507.         }
  508.         break;
  509.     case 'p':
  510.         if(c2=='c') {
  511. #ifdef m68851
  512.             if(c3 == 's' && c4=='r') {
  513.                 n=4;
  514.                 ret = (PCSR);
  515.             } else
  516. #endif
  517.             {
  518.                 n=2;
  519.                 ret = PC;
  520.             }
  521.         }
  522. #ifdef m68851
  523.         else if (c2 == 's' && c3 == 'r') {
  524.             n = 3;
  525.             ret = (PSR);
  526.         }
  527. #endif
  528.         break;
  529.     case 's':
  530. #ifdef m68851
  531.         if (c2 == 'c' && c3 == 'c') {
  532.             n = 3;
  533.             ret = (SCC);
  534.         } else if (c2 == 'r' && c3 == 'p') {
  535.             n = 3;
  536.             ret = (SRP);
  537.         } else
  538. #endif
  539.         if(c2=='r') {
  540.             n=2;
  541.             ret = SR;
  542.         } else if(c2=='p') {
  543.             n=2;
  544.             ret = ADDR+7;
  545.         } else if(c2=='f' && c3=='c') {
  546.             n=3;
  547.             ret = SFC;
  548.         }
  549.         break;
  550. #ifdef m68851
  551.     case 't':
  552.         if(c2 == 'c') {
  553.             n=2;
  554.             ret=TC;
  555.         }
  556.         break;
  557. #endif
  558.     case 'u':
  559.         if(c2=='s' && c3=='p') {
  560.             n=3;
  561.             ret = USP;
  562.         }
  563.         break;
  564.     case 'v':
  565. #ifdef m68851
  566.         if (c2 == 'a' && c3 == 'l') {
  567.             n = 3;
  568.             ret = (VAL);
  569.         } else
  570. #endif
  571.         if(c2=='b' && c3=='r') {
  572.             n=3;
  573.             ret = VBR;
  574.         }
  575.         break;
  576.     case 'z':
  577.         if(c2=='p' && c3=='c') {
  578.             n=3;
  579.             ret = ZPC;
  580.         }
  581.         break;
  582.     default:
  583.         break;
  584.     }
  585.     if(n) {
  586.         if(isalnum(ccp[0][n]) || ccp[0][n]=='_')
  587.             ret=FAIL;
  588.         else
  589.             ccp[0]+=n;
  590.     } else
  591.         ret = FAIL;
  592.     return ret;
  593. }
  594.  
  595. #define SKIP_WHITE()    { str++; if(*str==' ') str++;}
  596.  
  597. m68k_ip_op(str,opP)
  598. char *str;
  599. register struct m68k_op *opP;
  600. {
  601.     char    *strend;
  602.     long    i;
  603.     char    *parse_index();
  604.  
  605.     if(*str==' ')
  606.         str++;
  607.         /* Find the end of the string */
  608.     if(!*str) {
  609.         /* Out of gas */
  610.         opP->error="Missing operand";
  611.         return FAIL;
  612.     }
  613.     for(strend=str;*strend;strend++)
  614.         ;
  615.     --strend;
  616.  
  617.         /* Guess what:  A constant.  Shar and enjoy */
  618.     if(*str=='#') {
  619.         str++;
  620.         opP->con1=add_exp(str,strend);
  621.         opP->mode=IMMED;
  622.         return OK;
  623.     }
  624.     i=m68k_reg_parse(&str);
  625.     if((i==FAIL || *str!='\0') && *str!='@') {
  626.         char *stmp;
  627.         char *index();
  628.  
  629.         if(i!=FAIL && (*str=='/' || *str=='-')) {
  630.             opP->mode=REGLST;
  631.             return get_regs(i,str,opP);
  632.         }
  633.         if(stmp=index(str,'@')) {
  634.             opP->con1=add_exp(str,stmp-1);
  635.             if(stmp==strend) {
  636.                 opP->mode=AINDX;
  637.                 return OK;
  638.             }
  639.             stmp++;
  640.             if(*stmp++!='(' || *strend--!=')') {
  641.                 opP->error="Malformed operand";
  642.                 return FAIL;
  643.             }
  644.             i=try_index(&stmp,opP);
  645.             opP->con2=add_exp(stmp,strend);
  646.             if(i==FAIL) opP->mode=AMIND;
  647.             else opP->mode=APODX;
  648.             return OK;
  649.         }
  650.         opP->mode=ABSL;
  651.         if(strend[-1]==':') {    /* mode ==foo:[wl] */
  652.             switch(*strend) {
  653.             case 'w':
  654.             case 'W':
  655.                 opP->isiz=2;
  656.                 break;
  657.             case 'l':
  658.             case 'L':
  659.                 opP->isiz=3;
  660.                 break;
  661.             default:
  662.                 opP->error="size spec not :w or :l";
  663.                 return FAIL;
  664.             }
  665.             strend-=2;
  666.         } else {
  667.             opP->isiz=0;
  668.         }
  669.  
  670.         opP->con1=add_exp(str,strend);
  671.         return OK;
  672.     }
  673.     opP->reg=i;
  674.     if(*str=='\0') {
  675.         if(i>=DATA+0 && i<=DATA+7)
  676.             opP->mode=DREG;
  677.         else if(i>=ADDR+0 && i<=ADDR+7)
  678.             opP->mode=AREG;
  679.         else
  680.             opP->mode=MSCR;
  681.         return OK;
  682.     }
  683.     if((i<ADDR+0 || i>ADDR+7) && i!=PC && i!=ZPC && i!=FAIL) {    /* Can't indirect off non address regs */
  684.         opP->error="Invalid indirect register";
  685.         return FAIL;
  686.     }
  687.     if(*str!='@')
  688.         abort();
  689.     str++;
  690.     switch(*str) {
  691.     case '\0':
  692.         opP->mode=AINDR;
  693.         return OK;
  694.     case '-':
  695.         opP->mode=ADEC;
  696.         return OK;
  697.     case '+':
  698.         opP->mode=AINC;
  699.         return OK;
  700.     case '(':
  701.         str++;
  702.         break;
  703.     default:
  704.         opP->error="Junk after indirect";
  705.         return FAIL;
  706.     }
  707.         /* Some kind of indexing involved.  Lets find out how bad it is */
  708.     i=try_index(&str,opP);
  709.         /* Didn't start with an index reg, maybe its offset or offset,reg */
  710.     if(i==FAIL) {
  711.         char *beg_str;
  712.  
  713.         beg_str=str;
  714.         for(i=1;i;) {
  715.             switch(*str++) {
  716.             case '\0':
  717.                 opP->error="Missing )";
  718.                 return FAIL;
  719.             case ',': i=0; break;
  720.             case '(': i++; break;
  721.             case ')': --i; break;
  722.             }
  723.         }
  724.         opP->con1=add_exp(beg_str,str-2);
  725.             /* Should be offset,reg */
  726.         if(str[-1]==',') {
  727.             i=try_index(&str,opP);
  728.             if(i==FAIL) {
  729.                 opP->error="Malformed index reg";
  730.                 return FAIL;
  731.             }
  732.         }
  733.     }
  734.         /* We've now got offset)   offset,reg)   or    reg) */
  735.  
  736.     if(*str=='\0') {
  737.         /* Th-the-thats all folks */
  738.         if(opP->reg==FAIL) opP->mode=AINDX;    /* Other form of indirect */
  739.         else if(opP->ireg==FAIL) opP->mode=AOFF;
  740.         else opP->mode=AINDX;
  741.         return OK;
  742.     }
  743.         /* Next thing had better be another @ */
  744.     if(*str!='@' || str[1]!='(') {
  745.         opP->error="junk after indirect";
  746.         return FAIL;
  747.     }
  748.     str+=2;
  749.     if(opP->ireg!=FAIL) {
  750.         opP->mode=APRDX;
  751.         i=try_index(&str,opP);
  752.         if(i!=FAIL) {
  753.             opP->error="Two index registers!  not allowed!";
  754.             return FAIL;
  755.         }
  756.     } else
  757.         i=try_index(&str,opP);
  758.     if(i==FAIL) {
  759.         char *beg_str;
  760.  
  761.         beg_str=str;
  762.         for(i=1;i;) {
  763.             switch(*str++) {
  764.             case '\0':
  765.                 opP->error="Missing )";
  766.                 return FAIL;
  767.             case ',': i=0; break;
  768.             case '(': i++; break;
  769.             case ')': --i; break;
  770.             }
  771.         }
  772.         opP->con2=add_exp(beg_str,str-2);
  773.         if(str[-1]==',') {
  774.             if(opP->ireg!=FAIL) {
  775.                 opP->error="Can't have two index regs";
  776.                 return FAIL;
  777.             }
  778.             i=try_index(&str,opP);
  779.             if(i==FAIL) {
  780.                 opP->error="malformed index reg";
  781.                 return FAIL;
  782.             }
  783.             opP->mode=APODX;
  784.         } else if(opP->ireg!=FAIL)
  785.             opP->mode=APRDX;
  786.         else
  787.             opP->mode=AMIND;
  788.     } else
  789.         opP->mode=APODX;
  790.     if(*str!='\0') {
  791.         opP->error="Junk after indirect";
  792.         return FAIL;
  793.     }
  794.     return OK;
  795. }
  796.  
  797. try_index(s,opP)
  798. char **s;
  799. struct m68k_op *opP;
  800. {
  801.     register int    i;
  802.     char    *ss;
  803. #define SKIP_W()    { ss++; if(*ss==' ') ss++;}
  804.  
  805.     ss= *s;
  806.     /* SKIP_W(); */
  807.     i=m68k_reg_parse(&ss);
  808.     if(!(i>=DATA+0 && i<=ADDR+7)) {    /* if i is not DATA or ADDR reg */
  809.         *s=ss;
  810.         return FAIL;
  811.     }
  812.     opP->ireg=i;
  813.     /* SKIP_W(); */
  814.     if(*ss==')') {
  815.         opP->isiz=0;
  816.         opP->imul=1;
  817.         SKIP_W();
  818.         *s=ss;
  819.         return OK;
  820.     }
  821.     if(*ss!=':') {
  822.         opP->error="Missing : in index register";
  823.         *s=ss;
  824.         return FAIL;
  825.     }
  826.     SKIP_W();
  827.     if(mklower(*ss)=='w') opP->isiz=2;
  828.     else if(mklower(*ss)=='l') opP->isiz=3;
  829.     else {
  830.         opP->error="Size spec not :w or :l";
  831.         *s=ss;
  832.         return FAIL;
  833.     }
  834.     SKIP_W();
  835.     if(*ss==':') {
  836.         SKIP_W();
  837.         switch(*ss) {
  838.         case '1':
  839.         case '2':
  840.         case '4':
  841.         case '8':
  842.             opP->imul= *ss-'0';
  843.             break;
  844.         default:
  845.             opP->error="index multiplier not 1, 2, 4 or 8";
  846.             *s=ss;
  847.             return FAIL;
  848.         }
  849.         SKIP_W();
  850.     } else opP->imul=1;
  851.     if(*ss!=')') {
  852.         opP->error="Missing )";
  853.         *s=ss;
  854.         return FAIL;
  855.     }
  856.     SKIP_W();
  857.     *s=ss;
  858.     return OK;
  859. }
  860.  
  861. #ifdef TEST1    /* TEST1 tests m68k_ip_op(), which parses operands */
  862. main()
  863. {
  864.     char buf[128];
  865.     struct m68k_op thark;
  866.  
  867.     for(;;) {
  868.         if(!gets(buf))
  869.             break;
  870.         bzero(&thark,sizeof(thark));
  871.         if(!m68k_ip_op(buf,&thark)) printf("FAIL:");
  872.         if(thark.error)
  873.             printf("op1 error %s in %s\n",thark.error,buf);
  874.         printf("mode %d, reg %d, ",thark.mode,thark.reg);
  875.         if(thark.b_const)
  876.             printf("Constant: '%.*s',",1+thark.e_const-thark.b_const,thark.b_const);
  877.         printf("ireg %d, isiz %d, imul %d ",thark.ireg,thark.isiz,thark.imul);
  878.         if(thark.b_iadd)
  879.             printf("Iadd: '%.*s'",1+thark.e_iadd-thark.b_iadd,thark.b_iadd);
  880.         printf("\n");
  881.     }
  882.     exit(0);
  883. }
  884.  
  885. #endif
  886.  
  887.  
  888. static struct hash_control*   op_hash = NULL;    /* handle of the OPCODE hash table
  889.                    NULL means any use before m68_ip_begin()
  890.                    will crash */
  891.  
  892.  
  893. /*
  894.  *                  m 6 8 _ i p ( )
  895.  *
  896.  * This converts a string into a 68k instruction.
  897.  * The string must be a bare single instruction in sun format
  898.  * with RMS-style 68020 indirects
  899.  *  (example:  )
  900.  *
  901.  * It provides some error messages: at most one fatal error message (which
  902.  * stops the scan) and at most one warning message for each operand.
  903.  * The 68k instruction is returned in exploded form, since we have no
  904.  * knowledge of how you parse (or evaluate) your expressions.
  905.  * We do however strip off and decode addressing modes and operation
  906.  * mnemonic.
  907.  *
  908.  * This function's value is a string. If it is not "" then an internal
  909.  * logic error was found: read this code to assign meaning to the string.
  910.  * No argument string should generate such an error string:
  911.  * it means a bug in our code, not in the user's text.
  912.  *
  913.  * You MUST have called m86_ip_begin() once and m86_ip_end() never before using
  914.  * this function.
  915.  */
  916.  
  917. /* JF this function no longer returns a useful value.  Sorry */
  918. char *
  919. m68_ip (instring)
  920. char    *instring;
  921. {
  922.     register char *p;
  923.     register struct m68k_op *opP;
  924.     register struct m68_incant *opcode;
  925.     register char *s;
  926.     register int tmpreg,
  927.         baseo,
  928.         outro,
  929.         nextword;
  930.     int    siz1,
  931.         siz2;
  932.     char    c;
  933.     int    losing;
  934.     int    opsfound;
  935.     char    *crack_operand();
  936.     char    *atof_m68k();
  937.     LITTLENUM_TYPE words[6];
  938.     LITTLENUM_TYPE *wordp;
  939.  
  940.     if (*instring == ' ')
  941.         instring++;            /* skip leading whitespace */
  942.  
  943.   /* Scan up to end of operation-code, which MUST end in end-of-string
  944.      or exactly 1 space. */
  945.     for (p = instring; *p != '\0'; p++)
  946.         if (*p == ' ')
  947.             break;
  948.  
  949.  
  950.     if (p == instring) {
  951.         the_ins.error = "No operator";
  952.         the_ins.opcode[0] = NULL;
  953.         /* the_ins.numo=1; */
  954.         return;
  955.     }
  956.  
  957.   /* p now points to the end of the opcode name, probably whitespace.
  958.      make sure the name is null terminated by clobbering the whitespace,
  959.      look it up in the hash table, then fix it back. */   
  960.     c = *p;
  961.     *p = '\0';
  962.     opcode = (struct m68_incant *)hash_find (op_hash, instring);
  963.     *p = c;
  964.  
  965.     if (opcode == NULL) {
  966.         the_ins.error = "Unknown operator";
  967.         the_ins.opcode[0] = NULL;
  968.         /* the_ins.numo=1; */
  969.         return;
  970.     }
  971.  
  972.   /* found a legitimate opcode, start matching operands */
  973.     for(opP= &the_ins.operands[0];*p;opP++) {
  974.         p = crack_operand (p, opP);
  975.         if(opP->error) {
  976.             the_ins.error=opP->error;
  977.             return;
  978.         }
  979.     }
  980.  
  981.     opsfound=opP- &the_ins.operands[0];
  982.     /* This ugly hack is to support the floating pt opcodes in their standard form */
  983.     /* Essentially, we fake a first enty of type COP#1 */
  984.     if(opcode->m_operands[0]=='I') {
  985.         int    n;
  986.  
  987.         for(n=opsfound;n>0;--n)
  988.             the_ins.operands[n]=the_ins.operands[n-1];
  989.  
  990.         /* bcopy((char *)(&the_ins.operands[0]),(char *)(&the_ins.operands[1]),opsfound*sizeof(the_ins.operands[0])); */
  991.         bzero((char *)(&the_ins.operands[0]),sizeof(the_ins.operands[0]));
  992.         the_ins.operands[0].mode=MSCR;
  993.         the_ins.operands[0].reg=COPNUM;        /* COP #1 */
  994.         opsfound++;
  995.     }
  996.         /* We've got the operands.  Find an opcode that'll
  997.            accept them */
  998.     for(losing=0;;) {
  999.         if(opsfound!=opcode->m_opnum)
  1000.             losing++;
  1001.         else for(s=opcode->m_operands,opP= &the_ins.operands[0];*s && !losing;s+=2,opP++) {
  1002.                 /* Warning: this switch is huge! */
  1003.                 /* I've tried to organize the cases into  this order:
  1004.                    non-alpha first, then alpha by letter.  lower-case goes directly
  1005.                    before uppercase counterpart. */
  1006.                 /* Code with multiple case ...: gets sorted by the lowest case ...
  1007.                    it belongs to.  I hope this makes sense. */
  1008.             switch(*s) {
  1009.             case '!':
  1010.                 if(opP->mode==MSCR || opP->mode==IMMED ||
  1011.  opP->mode==DREG || opP->mode==AREG || opP->mode==AINC || opP->mode==ADEC || opP->mode==REGLST)
  1012.                     losing++;
  1013.                 break;
  1014.  
  1015.             case '#':
  1016.                 if(opP->mode!=IMMED)
  1017.                      losing++;
  1018.                 else {
  1019.                     long t;
  1020.  
  1021.                     t=get_num(opP->con1,80);
  1022.                     if(s[1]=='b' && !isbyte(t))
  1023.                         losing++;
  1024.                     else if(s[1]=='w' && !isword(t))
  1025.                         losing++;
  1026.                 }
  1027.                 break;
  1028.  
  1029.             case '^':
  1030.             case 'T':
  1031.                 if(opP->mode!=IMMED)
  1032.                     losing++;
  1033.                 break;
  1034.  
  1035.             case '$':
  1036.                 if(opP->mode==MSCR || opP->mode==AREG ||
  1037.  opP->mode==IMMED || opP->reg==PC || opP->reg==ZPC || opP->mode==REGLST)
  1038.                     losing++;
  1039.                 break;
  1040.  
  1041.             case '%':
  1042.                 if(opP->mode==MSCR || opP->reg==PC ||
  1043.  opP->reg==ZPC || opP->mode==REGLST)
  1044.                     losing++;
  1045.                 break;
  1046.  
  1047.  
  1048.             case '&':
  1049.                 if(opP->mode==MSCR || opP->mode==DREG ||
  1050.  opP->mode==AREG || opP->mode==IMMED || opP->reg==PC || opP->reg==ZPC ||
  1051.  opP->mode==AINC || opP->mode==ADEC || opP->mode==REGLST)
  1052.                     losing++;
  1053.                 break;
  1054.  
  1055.             case '*':
  1056.                 if(opP->mode==MSCR || opP->mode==REGLST)
  1057.                     losing++;
  1058.                 break;
  1059.  
  1060.             case '+':
  1061.                 if(opP->mode!=AINC)
  1062.                     losing++;
  1063.                 break;
  1064.  
  1065.             case '-':
  1066.                 if(opP->mode!=ADEC)
  1067.                     losing++;
  1068.                 break;
  1069.  
  1070.             case '/':
  1071.                 if(opP->mode==MSCR || opP->mode==AREG ||
  1072.  opP->mode==AINC || opP->mode==ADEC || opP->mode==IMMED || opP->mode==REGLST)
  1073.                     losing++;
  1074.                 break;
  1075.  
  1076.             case ';':
  1077.                 if(opP->mode==MSCR || opP->mode==AREG || opP->mode==REGLST)
  1078.                     losing++;
  1079.                 break;
  1080.  
  1081.             case '?':
  1082.                 if(opP->mode==MSCR || opP->mode==AREG ||
  1083.  opP->mode==AINC || opP->mode==ADEC || opP->mode==IMMED || opP->reg==PC ||
  1084.  opP->reg==ZPC || opP->mode==REGLST)
  1085.                     losing++;
  1086.                 break;
  1087.  
  1088.             case '@':
  1089.                 if(opP->mode==MSCR || opP->mode==AREG ||
  1090.  opP->mode==IMMED || opP->mode==REGLST)
  1091.                     losing++;
  1092.                 break;
  1093.  
  1094.             case '~':        /* For now! (JF FOO is this right?) */
  1095.                 if(opP->mode==MSCR || opP->mode==DREG ||
  1096.  opP->mode==AREG || opP->mode==IMMED || opP->reg==PC || opP->reg==ZPC || opP->mode==REGLST)
  1097.                     losing++;
  1098.                 break;
  1099.  
  1100.             case 'A':
  1101.                 if(opP->mode!=AREG)
  1102.                     losing++;
  1103.                 break;
  1104.  
  1105.             case 'B':    /* FOO */
  1106.                 if(opP->mode!=ABSL)
  1107.                     losing++;
  1108.                 break;
  1109.  
  1110.             case 'C':
  1111.                 if(opP->mode!=MSCR || opP->reg!=CCR)
  1112.                     losing++;
  1113.                 break;
  1114.  
  1115.             case 'd':    /* FOO This mode is a KLUDGE!! */
  1116.                 if(opP->mode!=AOFF && (opP->mode!=ABSL ||
  1117.  opP->con1->e_beg[0]!='(' || opP->con1->e_end[0]!=')'))
  1118.                     losing++;
  1119.                 break;
  1120.  
  1121.             case 'D':
  1122.                 if(opP->mode!=DREG)
  1123.                     losing++;
  1124.                 break;
  1125.  
  1126.             case 'F':
  1127.                 if(opP->mode!=MSCR || opP->reg<(FPREG+0) || opP->reg>(FPREG+7))
  1128.                     losing++;
  1129.                 break;
  1130.  
  1131.             case 'I':
  1132.                 if(opP->mode!=MSCR || opP->reg<COPNUM ||
  1133.  opP->reg>=COPNUM+7)
  1134.                     losing++;
  1135.                 break;
  1136.  
  1137.             case 'J':
  1138.                 if(opP->mode!=MSCR || opP->reg<USP || opP->reg>MSP)
  1139.                     losing++;
  1140.                 break;
  1141.  
  1142.             case 'k':
  1143.                 if(opP->mode!=IMMED)
  1144.                     losing++;
  1145.                 break;
  1146.  
  1147.             case 'l':
  1148.             case 'L':
  1149.                 if(opP->mode==DREG || opP->mode==AREG || opP->mode==FPREG) {
  1150.                     if(s[1]=='8')
  1151.                         losing++;
  1152.                     else {
  1153.                         opP->mode=REGLST;
  1154.                         opP->reg=1<<(opP->reg-DATA);
  1155.                     }
  1156.                 } else if(opP->mode!=REGLST) {
  1157.                     losing++;
  1158.                 } else if(s[1]=='8' && opP->reg&0x0FFffFF)
  1159.                     losing++;
  1160.                 else if(s[1]=='3' && opP->reg&0x7000000)
  1161.                     losing++;
  1162.                 break;
  1163.  
  1164.             case 'M':
  1165.                 if(opP->mode!=IMMED)
  1166.                     losing++;
  1167.                 else {
  1168.                     long t;
  1169.  
  1170.                     t=get_num(opP->con1,80);
  1171.                     if(!issbyte(t) || isvar(opP->con1))
  1172.                         losing++;
  1173.                 }
  1174.                 break;
  1175.  
  1176.             case 'O':
  1177.                 if(opP->mode!=DREG && opP->mode!=IMMED)
  1178.                     losing++;
  1179.                 break;
  1180.  
  1181.             case 'Q':
  1182.                 if(opP->mode!=IMMED)
  1183.                     losing++;
  1184.                 else {
  1185.                     long t;
  1186.  
  1187.                     t=get_num(opP->con1,80);
  1188.                     if(t<1 || t>8 || isvar(opP->con1))
  1189.                         losing++;
  1190.                 }
  1191.                 break;
  1192.  
  1193.             case 'R':
  1194.                 if(opP->mode!=DREG && opP->mode!=AREG)
  1195.                     losing++;
  1196.                 break;
  1197.  
  1198.             case 's':
  1199.                 if(opP->mode!=MSCR || !(opP->reg==FPI || opP->reg==FPS || opP->reg==FPC))
  1200.                     losing++;
  1201.                 break;
  1202.  
  1203.             case 'S':
  1204.                 if(opP->mode!=MSCR || opP->reg!=SR)
  1205.                     losing++;
  1206.                 break;
  1207.  
  1208.             case 'U':
  1209.                 if(opP->mode!=MSCR || opP->reg!=USP)
  1210.                     losing++;
  1211.                 break;
  1212.  
  1213.             /* JF these are out of order.  We could put them
  1214.                in order if we were willing to put up with
  1215.                bunches of #ifdef m68851s in the code */
  1216. #ifdef m68851
  1217.             /* Memory addressing mode used by pflushr */
  1218.             case '|':
  1219.                 if(opP->mode==MSCR || opP->mode==DREG ||
  1220.  opP->mode==AREG || opP->mode==REGLST)
  1221.                     losing++;
  1222.                 break;
  1223.  
  1224.             case 'f':
  1225.                 if (opP->mode != MSCR || (opP->reg != SFC && opP->reg != DFC))
  1226.                     losing++;
  1227.                 break;
  1228.  
  1229.             case 'P':
  1230.                 if (opP->mode != MSCR || (opP->reg != TC && opP->reg != CAL &&
  1231.                     opP->reg != VAL && opP->reg != SCC && opP->reg != AC))
  1232.                     losing++;
  1233.                 break;
  1234.  
  1235.             case 'V':
  1236.                 if (opP->reg != VAL)
  1237.                     losing++;
  1238.                 break;
  1239.  
  1240.             case 'W':
  1241.                 if (opP->mode != MSCR || (opP->reg != DRP && opP->reg != SRP &&
  1242.                     opP->reg != CRP))
  1243.                     losing++;
  1244.                 break;
  1245.  
  1246.             case 'X':
  1247.                 if (opP->mode != MSCR ||
  1248.                     (!(opP->reg >= BAD && opP->reg <= BAD+7) &&
  1249.                      !(opP->reg >= BAC && opP->reg <= BAC+7)))
  1250.                     losing++;
  1251.                 break;
  1252.  
  1253.             case 'Y':
  1254.                 if (opP->reg != PSR)
  1255.                     losing++;
  1256.                 break;
  1257.  
  1258.             case 'Z':
  1259.                 if (opP->reg != PCSR)
  1260.                     losing++;
  1261.                 break;
  1262. #endif
  1263.             default:
  1264.                 as_fatal("Internal error:  Operand mode %c unknown",*s);
  1265.             }
  1266.         }
  1267.         if(!losing)
  1268.             break;
  1269.         opcode=opcode->m_next;
  1270.         if(!opcode) {        /* Fell off the end */
  1271.             the_ins.error="instruction/operands mismatch";
  1272.             return;
  1273.         }
  1274.         losing=0;
  1275.     }
  1276.     the_ins.args=opcode->m_operands;
  1277.     the_ins.numargs=opcode->m_opnum;
  1278.     the_ins.numo=opcode->m_codenum;
  1279.     the_ins.opcode[0]=getone(opcode);
  1280.     the_ins.opcode[1]=gettwo(opcode);
  1281.  
  1282.     for(s=the_ins.args,opP= &the_ins.operands[0];*s;s+=2,opP++) {
  1283.             /* This switch is a doozy.
  1284.                What the first step; its a big one! */
  1285.         switch(s[0]) {
  1286.  
  1287.         case '*':
  1288.         case '~':
  1289.         case '%':
  1290.         case ';':
  1291.         case '@':
  1292.         case '!':
  1293.         case '&':
  1294.         case '$':
  1295.         case '?':
  1296.         case '/':
  1297. #ifdef m68851
  1298.         case '|':
  1299. #endif
  1300.             switch(opP->mode) {
  1301.             case IMMED:
  1302.                 tmpreg=0x3c;    /* 7.4 */
  1303.                 if(index("bwl",s[1])) nextword=get_num(opP->con1,80);
  1304.                 else nextword=nextword=get_num(opP->con1,0);
  1305.                 if(isvar(opP->con1))
  1306.                     add_fix(s[1],opP->con1,0);
  1307.                 switch(s[1]) {
  1308.                 case 'b':
  1309.                     if(!isbyte(nextword))
  1310.                         opP->error="operand out of range";
  1311.                     addword(nextword);
  1312.                     baseo=0;
  1313.                     break;
  1314.                 case 'w':
  1315.                     if(!isword(nextword))
  1316.                         opP->error="operand out of range";
  1317.                     addword(nextword);
  1318.                     baseo=0;
  1319.                     break;
  1320.                 case 'l':
  1321.                     addword(nextword>>16);
  1322.                     addword(nextword);
  1323.                     baseo=0;
  1324.                     break;
  1325.  
  1326.                 case 'f':
  1327.                     baseo=2;
  1328.                     outro=8;
  1329.                     break;
  1330.                 case 'F':
  1331.                     baseo=4;
  1332.                     outro=11;
  1333.                     break;
  1334.                 case 'x':
  1335.                     baseo=6;
  1336.                     outro=15;
  1337.                     break;
  1338.                 case 'p':
  1339.                     baseo=6;
  1340.                     outro= -1;
  1341.                     break;
  1342.                 default:
  1343.                     as_fatal("Internal error:  Can't decode %c%c",*s,s[1]);
  1344.                 }
  1345.                 if(!baseo)
  1346.                     break;
  1347.  
  1348.                 /* We gotta put out some float */
  1349.                 if(seg(opP->con1)!=SEG_BIG) {
  1350.                     int_to_gen(nextword);
  1351.                     gen_to_words(words,baseo,(long int)outro);
  1352.                     for(wordp=words;baseo--;wordp++)
  1353.                         addword(*wordp);
  1354.                     break;
  1355.                 }        /* Its BIG */
  1356.                 if(offs(opP->con1)>0) {
  1357.                     as_warn("Bignum assumed to be binary bit-pattern");
  1358.                     if(offs(opP->con1)>baseo) {
  1359.                         as_warn("Bignum too big for %c format; truncated",s[1]);
  1360.                         offs(opP->con1)=baseo;
  1361.                     }
  1362.                     baseo-=offs(opP->con1);
  1363.                     for(wordp=generic_bignum;offs(opP->con1)--;wordp++)
  1364.                         addword(*wordp);
  1365.                     while(baseo--)
  1366.                         addword(0);
  1367.                     break;
  1368.                 }
  1369.                 gen_to_words(words,baseo,(long int)outro);
  1370.                 for(wordp=words;baseo--;wordp++)
  1371.                     addword(*wordp);
  1372.                 break;
  1373.             case DREG:
  1374.                 tmpreg=opP->reg-DATA; /* 0.dreg */
  1375.                 break;
  1376.             case AREG:
  1377.                 tmpreg=0x08+opP->reg-ADDR; /* 1.areg */
  1378.                 break;
  1379.             case AINDR:
  1380.                 tmpreg=0x10+opP->reg-ADDR; /* 2.areg */
  1381.                 break;
  1382.             case ADEC:
  1383.                 tmpreg=0x20+opP->reg-ADDR; /* 4.areg */
  1384.                 break;
  1385.             case AINC:
  1386.                 tmpreg=0x18+opP->reg-ADDR; /* 3.areg */
  1387.                 break;
  1388.             case AOFF:
  1389.                 if(opP->reg==PC)
  1390.                     tmpreg=0x3A; /* 7.2 */
  1391.                 else
  1392.                     tmpreg=0x28+opP->reg-ADDR; /* 5.areg */
  1393.                 nextword=get_num(opP->con1,80);
  1394.                 /* Force into index mode.  Hope this works */
  1395.                 if(!issword(nextword)) {
  1396.                     if(opP->reg==PC)
  1397.                         tmpreg=0x3B;    /* 7.3 */
  1398.                     else
  1399.                         tmpreg=0x30+opP->reg-ADDR;    /* 6.areg */
  1400.                     /* addword(0x0171); */
  1401. /* 171 seems to be wrong, and I can't find the 68020 manual, so we'll try 170
  1402.    (which is what the Sun asm seems to generate */
  1403.                     addword(0x0170);
  1404.                     if(isvar(opP->con1))
  1405.                         add_fix('l',opP->con1,0);
  1406.                     addword(nextword>>16);
  1407.                     /* addword(nextword); */
  1408.                 } else if(isvar(opP->con1)) {
  1409.                     if(!flagseen['l']) {
  1410.                         add_fix('w',opP->con1,0);
  1411.                     } else {
  1412.                         tmpreg=0x30+opP->reg-ADDR;
  1413.                         addword(0x0170);
  1414.                         add_fix('l',opP->con1,0);
  1415.                         addword(nextword>>16);
  1416.                     }
  1417.                 }
  1418.                 addword(nextword);
  1419.                 break;
  1420.             case AINDX:
  1421.             case APODX:
  1422.             case AMIND:
  1423.             case APRDX:
  1424.                 nextword=0;
  1425.                 baseo=get_num(opP->con1,80);
  1426.                 outro=get_num(opP->con2,80);
  1427.                     /* Figure out the 'addressing mode' */
  1428.                     /* Also turn on the BASE_DISABLE bit, if needed */
  1429.                 if(opP->reg==PC || opP->reg==ZPC) {
  1430.                     tmpreg=0x3b; /* 7.3 */
  1431.                     if(opP->reg==ZPC)
  1432.                         nextword|=0x80;
  1433.                 } else if(opP->reg==FAIL) {
  1434.                     nextword|=0x80;
  1435.                     tmpreg=0x30;    /* 6.garbage */
  1436.                 } else tmpreg=0x30+opP->reg-ADDR; /* 6.areg */
  1437.  
  1438.                 siz1= (opP->con1) ? opP->con1->e_siz : 0;
  1439.                 siz2= (opP->con2) ? opP->con2->e_siz : 0;
  1440.  
  1441.                     /* Index register stuff */
  1442.                 if(opP->ireg>=DATA+0 && opP->ireg<=ADDR+7) {
  1443.                     nextword|=(opP->ireg-DATA)<<12;
  1444.  
  1445.                     if(opP->isiz==0 || opP->isiz==3)
  1446.                         nextword|=0x800;
  1447.                     switch(opP->imul) {
  1448.                     case 1: break;
  1449.                     case 2: nextword|=0x200; break;
  1450.                     case 4: nextword|=0x400; break;
  1451.                     case 8: nextword|=0x600; break;
  1452.                     default: abort();
  1453.                     }
  1454.                         /* IF its simple, GET US OUT OF HERE! */
  1455.                         /* Must be INDEX, with an index register.  Address register
  1456.                            cannot be ZERO-PC, and either :b was forced, or we know it'll fit */
  1457.                     if(opP->mode==AINDX &&
  1458.  opP->reg!=FAIL && opP->reg!=ZPC && (siz1==1 || (issbyte(baseo) &&
  1459.  !isvar(opP->con1)))) {
  1460.                         nextword +=baseo&0xff;
  1461.                         addword(nextword);
  1462.                         if(isvar(opP->con1))
  1463.                             add_fix('B',opP->con1,0);
  1464.                         break;
  1465.                     }
  1466.                 } else nextword|=0x40;    /* No index reg */
  1467.                 
  1468.                     /* It aint simple */
  1469.                 nextword|=0x100;
  1470.                     /* If the guy specified a width, we assume that
  1471.                        it is wide enough.  Maybe it isn't.  Ifso, we lose
  1472.                      */
  1473.                 switch(siz1) {
  1474.                 case 0:
  1475.                     if(isvar(opP->con1) || !issword(baseo)) {
  1476.                         siz1=3;
  1477.                         nextword|=0x30;
  1478.                     } else if(baseo==0)
  1479.                         nextword|=0x10;
  1480.                     else {    
  1481.                         nextword|=0x20;
  1482.                         siz1=2;
  1483.                     }
  1484.                     break;
  1485.                 case 1:
  1486.                     as_warn("Byte dispacement won't work.  Defaulting to :w");
  1487.                 case 2:
  1488.                     nextword|=0x20;
  1489.                     break;
  1490.                 case 3:
  1491.                     nextword|=0x30;
  1492.                     break;
  1493.                 }
  1494.  
  1495.                     /* Figure out innner displacement stuff */
  1496.                 if(opP->mode!=AINDX) {
  1497.                     switch(siz2) {
  1498.                     case 0:
  1499.                         if(isvar(opP->con2) || !issword(outro)) {
  1500.                             siz2=3;
  1501.                             nextword|=0x3;
  1502.                         } else if(outro==0)
  1503.                             nextword|=0x1;
  1504.                         else {    
  1505.                             nextword|=0x2;
  1506.                             siz2=2;
  1507.                         }
  1508.                         break;
  1509.                     case 1:
  1510.                         as_warn("Byte dispacement won't work.  Defaulting to :w");
  1511.                     case 2:
  1512.                         nextword|=0x2;
  1513.                         break;
  1514.                     case 3:
  1515.                         nextword|=0x3;
  1516.                         break;
  1517.                     }
  1518.                     if(opP->mode==APODX) nextword|=0x04;
  1519.                     else if(opP->mode==AMIND) nextword|=0x40;
  1520.                 }
  1521.                 addword(nextword);
  1522.  
  1523.                 if(isvar(opP->con1))
  1524.                     add_fix(siz1==3 ? 'l' : 'w',opP->con1,0);
  1525.                 if(siz1==3)
  1526.                     addword(baseo>>16);
  1527.                 if(siz1)
  1528.                     addword(baseo);
  1529.  
  1530.                 if(isvar(opP->con2))
  1531.                     add_fix(siz2==3 ? 'l' : 'w',opP->con2,0);
  1532.                 if(siz2==3)
  1533.                     addword(outro>>16);
  1534.                 if(siz2)
  1535.                     addword(outro);
  1536.  
  1537.                 break;
  1538.  
  1539.             case ABSL:
  1540.                 nextword=get_num(opP->con1,80);
  1541.                 switch(opP->isiz) {
  1542.                 case 0:
  1543.                     if(!isvar(opP->con1) && issword(offs(opP->con1))) {
  1544.                         tmpreg=0x38;
  1545.                         addword(nextword);
  1546.                         break;
  1547.                     }
  1548.                     if(isvar(opP->con1) &&
  1549.                        !subs(opP->con1) &&
  1550.                        seg(opP->con1)==SEG_TEXT &&
  1551.                         now_seg==SEG_TEXT) {
  1552.                         tmpreg=0x3A;
  1553.                         add_frag(adds(opP->con1),
  1554.                              offs(opP->con1),
  1555.                              TAB(PCREL,SZ_UNDEF));
  1556.                         break;
  1557.                     }
  1558.                 case 3:        /* Fall through into long */
  1559.                     if(isvar(opP->con1))
  1560.                         add_fix('l',opP->con1,0);
  1561.  
  1562.                     tmpreg=0x39;    /* 7.2 mode */
  1563.                     addword(nextword>>16);
  1564.                     addword(nextword);
  1565.                     break;
  1566.  
  1567.                 case 2:        /* Word */
  1568.                     if(isvar(opP->con1))
  1569.                         add_fix('w',opP->con1,0);
  1570.  
  1571.                     tmpreg=0x38;    /* 7.1 mode */
  1572.                     addword(nextword);
  1573.                     break;
  1574.                 }
  1575.                 break;
  1576.             case MSCR:
  1577.             default:
  1578.                 as_warn("unknown/incorrect operand");
  1579.                 /* abort(); */
  1580.             }
  1581.             install_gen_operand(s[1],tmpreg);
  1582.             break;
  1583.  
  1584.         case '#':
  1585.         case '^':
  1586.             switch(s[1]) {    /* JF: I hate floating point! */
  1587.             case 'j':
  1588.                 tmpreg=70;
  1589.                 break;
  1590.             case '8':
  1591.                 tmpreg=20;
  1592.                 break;
  1593.             case 'C':
  1594.                 tmpreg=50;
  1595.                 break;
  1596.             case '3':
  1597.             default:
  1598.                 tmpreg=80;
  1599.                 break;
  1600.             }
  1601.             tmpreg=get_num(opP->con1,tmpreg);
  1602.             if(isvar(opP->con1))
  1603.                 add_fix(s[1],opP->con1,0);
  1604.             switch(s[1]) {
  1605.             case 'b':    /* Danger:  These do no check for
  1606.                        certain types of overflow.
  1607.                        user beware! */
  1608.                 if(!isbyte(tmpreg))
  1609.                     opP->error="out of range";
  1610.                 insop(tmpreg);
  1611.                 if(isvar(opP->con1))
  1612.                     the_ins.reloc[the_ins.nrel-1].n=(opcode->m_codenum)*2;
  1613.                 break;
  1614.             case 'w':
  1615.                 if(!isword(tmpreg))
  1616.                     opP->error="out of range";
  1617.                 insop(tmpreg);
  1618.                 if(isvar(opP->con1))
  1619.                     the_ins.reloc[the_ins.nrel-1].n=(opcode->m_codenum)*2;
  1620.                 break;
  1621.             case 'l':
  1622.                 insop(tmpreg);        /* Because of the way insop works, we put these two out backwards */
  1623.                 insop(tmpreg>>16);
  1624.                 if(isvar(opP->con1))
  1625.                     the_ins.reloc[the_ins.nrel-1].n=(opcode->m_codenum)*2;
  1626.                 break;
  1627.             case '3':
  1628.                 tmpreg&=0xFF;
  1629.             case '8':
  1630.             case 'C':
  1631.                 install_operand(s[1],tmpreg);
  1632.                 break;
  1633.             default:
  1634.                 as_fatal("Internal error:  Unknown mode #%c",s[1]);
  1635.             }
  1636.             break;
  1637.  
  1638.         case '+':
  1639.         case '-':
  1640.         case 'A':
  1641.             install_operand(s[1],opP->reg-ADDR);
  1642.             break;
  1643.  
  1644.         case 'B':
  1645.             tmpreg=get_num(opP->con1,80);
  1646.             switch(s[1]) {
  1647.             case 'g':
  1648.                 if(opP->con1->e_siz) {    /* Deal with fixed size stuff by hand */
  1649.                     switch(opP->con1->e_siz) {
  1650.                     case 1:
  1651.                         add_fix('b',opP->con1,1);
  1652.                         break;
  1653.                     case 2:
  1654.                         add_fix('w',opP->con1,1);
  1655.                         addword(0);
  1656.                         break;
  1657.                     case 3:
  1658.                         add_fix('l',opP->con1,1);
  1659.                         addword(0);
  1660.                         addword(0);
  1661.                         break;
  1662.                     default:
  1663.                         as_fatal("Bad size for expression %d",opP->con1->e_siz);
  1664.                     }
  1665.                 } else if(subs(opP->con1)) {
  1666.                         /* We can't relax it */
  1667.                     the_ins.opcode[the_ins.numo]|=0xff;
  1668.                     add_fix('l',opP->con1,1);
  1669.                     addword(0);
  1670.                     addword(0);
  1671.                 } else if(adds(opP->con1)) {
  1672.                     add_frag(adds(opP->con1),offs(opP->con1),TAB(BRANCH,SZ_UNDEF));
  1673.                 } else {
  1674.                     add_frag((symbolS *)0,offs(opP->con1),TAB(BRANCH,BYTE));
  1675.                 }
  1676.                 break;
  1677.             case 'w':
  1678.                 if(isvar(opP->con1)) {
  1679.                         /* Don't ask! */
  1680.                     opP->con1->e_exp.X_add_number+=2;
  1681.                     add_fix('w',opP->con1,1);
  1682.                 }
  1683.                 addword(0);
  1684.                 break;
  1685.             case 'c':
  1686.                 if(opP->con1->e_siz) {
  1687.                     switch(opP->con1->e_siz) {
  1688.                     case 2:
  1689.                         add_fix('w',opP->con1,1)
  1690.                         addword(0);
  1691.                         break;
  1692.                     case 3:
  1693.                         the_ins.opcode[the_ins.numo]|=0x40;
  1694.                         add_fix('l',opP->con1,1);
  1695.                         addword(0);
  1696.                         addword(0);
  1697.                         break;
  1698.                     default:
  1699.                         as_warn("Bad size for offset, must be word or long");
  1700.                         break;
  1701.                     }
  1702.                 } else if(subs(opP->con1)) {
  1703.                     add_fix('l',opP->con1,1);
  1704.                     add_frag((symbolS *)0,(long)0,TAB(FBRANCH,LONG));
  1705.                 } else if(adds(opP->con1)) {
  1706.                     add_frag(adds(opP->con1),offs(opP->con1),TAB(FBRANCH,SZ_UNDEF));
  1707.                 } else {
  1708.                     add_frag((symbolS *)0,offs(opP->con1),TAB(FBRANCH,SHORT));
  1709.                 }
  1710.                 break;
  1711.             default:
  1712.                 as_fatal("Internal error:  operand type B%c unknown",s[1]);
  1713.             }
  1714.             break;
  1715.  
  1716.         case 'C':        /* Ignore it */
  1717.             break;
  1718.  
  1719.         case 'd':        /* JF this is a kludge */
  1720.             if(opP->mode==AOFF) {
  1721.                 install_operand('s',opP->reg-ADDR);
  1722.             } else {
  1723.                 char *tmpP;
  1724.  
  1725.                 tmpP=opP->con1->e_end-2;
  1726.                 opP->con1->e_beg++;
  1727.                 opP->con1->e_end-=4;    /* point to the , */
  1728.                 baseo=m68k_reg_parse(&tmpP);
  1729.                 if(baseo<ADDR+0 || baseo>ADDR+7) {
  1730.                     as_warn("Unknown address reg, using A0");
  1731.                     baseo=0;
  1732.                 } else baseo-=ADDR;
  1733.                 install_operand('s',baseo);
  1734.             }
  1735.             tmpreg=get_num(opP->con1,80);
  1736.             if(!issword(tmpreg)) {
  1737.                 as_warn("Expression out of range, using 0");
  1738.                 tmpreg=0;
  1739.             }
  1740.             addword(tmpreg);
  1741.             break;
  1742.  
  1743.         case 'D':
  1744.             install_operand(s[1],opP->reg-DATA);
  1745.             break;
  1746.  
  1747.         case 'F':
  1748.             install_operand(s[1],opP->reg-FPREG);
  1749.             break;
  1750.  
  1751.         case 'I':
  1752.             tmpreg=1+opP->reg-COPNUM;
  1753.             if(tmpreg==8)
  1754.                 tmpreg=0;
  1755.             install_operand(s[1],tmpreg);
  1756.             break;
  1757.  
  1758.         case 'J':        /* JF foo */
  1759.             switch(opP->reg) {
  1760.             case SFC:
  1761.                 tmpreg=0;
  1762.                 break;
  1763.             case DFC:
  1764.                 tmpreg=0x001;
  1765.                 break;
  1766.             case CACR:
  1767.                 tmpreg=0x002;
  1768.                 break;
  1769.             case USP:
  1770.                 tmpreg=0x800;
  1771.                 break;
  1772.             case VBR:
  1773.                 tmpreg=0x801;
  1774.                 break;
  1775.             case CAAR:
  1776.                 tmpreg=0x802;
  1777.                 break;
  1778.             case MSP:
  1779.                 tmpreg=0x803;
  1780.                 break;
  1781.             case ISP:
  1782.                 tmpreg=0x804;
  1783.                 break;
  1784.             default:
  1785.                 abort();
  1786.             }
  1787.             install_operand(s[1],tmpreg);
  1788.             break;
  1789.  
  1790.         case 'k':
  1791.             tmpreg=get_num(opP->con1,55);
  1792.             install_operand(s[1],tmpreg&0x7f);
  1793.             break;
  1794.  
  1795.         case 'l':
  1796.             tmpreg=opP->reg;
  1797.             if(s[1]=='w') {
  1798.                 if(tmpreg&0x7FF0000)
  1799.                     as_warn("Floating point register in register list");
  1800.                 insop(reverse_16_bits(tmpreg));
  1801.             } else {
  1802.                 if(tmpreg&0x700FFFF)
  1803.                     as_warn("Wrong register in floating-point reglist");
  1804.                 install_operand(s[1],reverse_8_bits(tmpreg>>16));
  1805.             }
  1806.             break;
  1807.  
  1808.         case 'L':
  1809.             tmpreg=opP->reg;
  1810.             if(s[1]=='w') {
  1811.                 if(tmpreg&0x7FF0000)
  1812.                     as_warn("Floating point register in register list");
  1813.                 insop(tmpreg);
  1814.             } else if(s[1]=='8') {
  1815.                 if(tmpreg&0x0FFFFFF)
  1816.                     as_warn("incorrect register in reglist");
  1817.                 install_operand(s[1],tmpreg>>24);
  1818.             } else {
  1819.                 if(tmpreg&0x700FFFF)
  1820.                     as_warn("wrong register in floating-point reglist");
  1821.                 else
  1822.                     install_operand(s[1],tmpreg>>16);
  1823.             }
  1824.             break;
  1825.  
  1826.         case 'M':
  1827.             install_operand(s[1],get_num(opP->con1,60));
  1828.             break;
  1829.  
  1830.         case 'O':
  1831.             tmpreg= (opP->mode==DREG) ? 0x20+opP->reg-DATA :
  1832.  get_num(opP->con1,40);
  1833.             install_operand(s[1],tmpreg);
  1834.             break;
  1835.  
  1836.         case 'Q':
  1837.             tmpreg=get_num(opP->con1,10);
  1838.             if(tmpreg==8)
  1839.                 tmpreg=0;
  1840.             install_operand(s[1],tmpreg);
  1841.             break;
  1842.  
  1843.         case 'R':
  1844.             /* This depends on the fact that ADDR registers are
  1845.                eight more than their corresponding DATA regs, so
  1846.                the result will have the ADDR_REG bit set */
  1847.             install_operand(s[1],opP->reg-DATA);
  1848.             break;
  1849.  
  1850.         case 's':
  1851.             if(opP->reg==FPI) tmpreg=0x1;
  1852.             else if(opP->reg==FPS) tmpreg=0x2;
  1853.             else if(opP->reg==FPC) tmpreg=0x4;
  1854.             else abort();
  1855.             install_operand(s[1],tmpreg);
  1856.             break;
  1857.  
  1858.         case 'S':    /* Ignore it */
  1859.             break;
  1860.  
  1861.         case 'T':
  1862.             install_operand(s[1],get_num(opP->con1,30));
  1863.             break;
  1864.  
  1865.         case 'U':    /* Ignore it */
  1866.             break;
  1867.  
  1868. #ifdef m68851
  1869.             /* JF: These are out of order, I fear. */
  1870.         case 'f':
  1871.             switch (opP->reg) {
  1872.             case SFC:
  1873.                 tmpreg=0;
  1874.                 break;
  1875.             case DFC:
  1876.                 tmpreg=1;
  1877.                 break;
  1878.             default:
  1879.                 abort();
  1880.             }
  1881.             install_operand(s[1],tmpreg);
  1882.             break;
  1883.  
  1884.         case 'P':
  1885.             switch(opP->reg) {
  1886.             case TC:
  1887.                 tmpreg=0;
  1888.                 break;
  1889.             case CAL:
  1890.                 tmpreg=4;
  1891.                 break;
  1892.             case VAL:
  1893.                 tmpreg=5;
  1894.                 break;
  1895.             case SCC:
  1896.                 tmpreg=6;
  1897.                 break;
  1898.             case AC:
  1899.                 tmpreg=7;
  1900.                 break;
  1901.             default:
  1902.                 abort();
  1903.             }
  1904.             install_operand(s[1],tmpreg);
  1905.             break;
  1906.  
  1907.         case 'V':
  1908.             if (opP->reg == VAL)
  1909.                 break;
  1910.             abort();
  1911.  
  1912.         case 'W':
  1913.             switch(opP->reg) {
  1914.  
  1915.             case DRP:
  1916.                 tmpreg=1;
  1917.                 break;
  1918.             case SRP:
  1919.                 tmpreg=2;
  1920.                 break;
  1921.             case CRP:
  1922.                 tmpreg=3;
  1923.                 break;
  1924.             default:
  1925.                 abort();
  1926.             }
  1927.             install_operand(s[1],tmpreg);
  1928.             break;
  1929.  
  1930.         case 'X':
  1931.             switch (opP->reg) {
  1932.             case BAD: case BAD+1: case BAD+2: case BAD+3:
  1933.             case BAD+4: case BAD+5: case BAD+6: case BAD+7:
  1934.                 tmpreg = (4 << 10) | ((opP->reg - BAD) << 2);
  1935.                 break;
  1936.  
  1937.             case BAC: case BAC+1: case BAC+2: case BAC+3:
  1938.             case BAC+4: case BAC+5: case BAC+6: case BAC+7:
  1939.                 tmpreg = (5 << 10) | ((opP->reg - BAC) << 2);
  1940.                 break;
  1941.  
  1942.             default:
  1943.                 abort();
  1944.             }
  1945.             install_operand(s[1], tmpreg);
  1946.             break;
  1947.         case 'Y':
  1948.             if (opP->reg == PSR)
  1949.                 break;
  1950.             abort();
  1951.  
  1952.         case 'Z':
  1953.             if (opP->reg == PCSR)
  1954.                 break;
  1955.             abort;
  1956. #endif /* m68851 */
  1957.         default:
  1958.             as_fatal("Internal error:  Operand type %c unknown",s[0]);
  1959.         }
  1960.     }
  1961.     /* By the time whe get here (FINALLY) the_ins contains the complete
  1962.        instruction, ready to be emitted. . . */
  1963. }
  1964.  
  1965.  
  1966. get_regs(i,str,opP)
  1967. struct m68k_op *opP;
  1968. char *str;
  1969. {
  1970.     /*                 26, 25, 24, 23-16,  15-8, 0-7 */
  1971.     /* Low order 24 bits encoded fpc,fps,fpi,fp7-fp0,a7-a0,d7-d0 */
  1972.     unsigned long int cur_regs = 0;
  1973.     int    reg1,
  1974.         reg2;
  1975.  
  1976. #define ADD_REG(x)    {     if(x==FPI) cur_regs|=(1<<24);\
  1977.              else if(x==FPS) cur_regs|=(1<<25);\
  1978.              else if(x==FPC) cur_regs|=(1<<26);\
  1979.              else cur_regs|=(1<<(x-1));  }
  1980.  
  1981.     reg1=i;
  1982.     for(;;) {
  1983.         if(*str=='/') {
  1984.             ADD_REG(reg1);
  1985.             str++;
  1986.         } else if(*str=='-') {
  1987.             str++;
  1988.             reg2=m68k_reg_parse(&str);
  1989.             if(reg2<DATA || reg2>=FPREG+8 || reg1==FPI || reg1==FPS || reg1==FPC) {
  1990.                 opP->error="unknown register in register list";
  1991.                 return FAIL;
  1992.             }
  1993.             while(reg1<=reg2) {
  1994.                 ADD_REG(reg1);
  1995.                 reg1++;
  1996.             }
  1997.             if(*str=='\0')
  1998.                 break;
  1999.         } else if(*str=='\0') {
  2000.             ADD_REG(reg1);
  2001.             break;
  2002.         } else {
  2003.             opP->error="unknow character in register list";
  2004.             return FAIL;
  2005.         }
  2006. /* DJA -- Bug Fix.  Did't handle d1-d2/a1 until the following instruction was added */
  2007.         if (*str=='/')
  2008.           str ++;
  2009.         reg1=m68k_reg_parse(&str);
  2010.         if((reg1<DATA || reg1>=FPREG+8) && !(reg1==FPI || reg1==FPS || reg1==FPC)) {
  2011.             opP->error="unknown register in register list";
  2012.             return FAIL;
  2013.         }
  2014.     }
  2015.     opP->reg=cur_regs;
  2016.     return OK;
  2017. }
  2018.  
  2019. int
  2020. reverse_16_bits(in)
  2021. {
  2022.     int out=0;
  2023.     int n;
  2024.  
  2025.     static int mask[16] = {
  2026. 0x0001,0x0002,0x0004,0x0008,0x0010,0x0020,0x0040,0x0080,
  2027. 0x0100,0x0200,0x0400,0x0800,0x1000,0x2000,0x4000,0x8000
  2028.     };
  2029.     for(n=0;n<16;n++) {
  2030.         if(in&mask[n])
  2031.             out|=mask[15-n];
  2032.     }
  2033.     return out;
  2034. }
  2035.  
  2036. int
  2037. reverse_8_bits(in)
  2038. {
  2039.     int out=0;
  2040.     int n;
  2041.  
  2042.     static int mask[8] = {
  2043. 0x0001,0x0002,0x0004,0x0008,0x0010,0x0020,0x0040,0x0080,
  2044.     };
  2045.  
  2046.     for(n=0;n<8;n++) {
  2047.         if(in&mask[n])
  2048.             out|=mask[7-n];
  2049.     }
  2050.     return out;
  2051. }
  2052.     
  2053.  
  2054. install_operand(mode,val)
  2055. {
  2056.     switch(mode) {
  2057.     case 's':
  2058.         the_ins.opcode[0]|=val & 0xFF;    /* JF FF is for M kludge */
  2059.         break;
  2060.     case 'd':
  2061.         the_ins.opcode[0]|=val<<9;
  2062.         break;
  2063.     case '1':
  2064.         the_ins.opcode[1]|=val<<12;
  2065.         break;
  2066.     case '2':
  2067.         the_ins.opcode[1]|=val<<6;
  2068.         break;
  2069.     case '3':
  2070.         the_ins.opcode[1]|=val;
  2071.         break;
  2072.     case '4':
  2073.         the_ins.opcode[2]|=val<<12;
  2074.         break;
  2075.     case '5':
  2076.         the_ins.opcode[2]|=val<<6;
  2077.         break;
  2078.     case '6':
  2079.             /* DANGER!  This is a hack to force cas2l and cas2w cmds
  2080.                to be three words long! */
  2081.         the_ins.numo++;
  2082.         the_ins.opcode[2]|=val;
  2083.         break;
  2084.     case '7':
  2085.         the_ins.opcode[1]|=val<<7;
  2086.         break;
  2087.     case '8':
  2088.         the_ins.opcode[1]|=val<<10;
  2089.         break;
  2090. #ifdef m68851
  2091.     case '9':
  2092.         the_ins.opcode[1]|=val<<5;
  2093.         break;
  2094. #endif
  2095.         
  2096.     case 't':
  2097.         the_ins.opcode[1]|=(val<<10)|(val<<7);
  2098.         break;
  2099.     case 'D':
  2100.         the_ins.opcode[1]|=(val<<12)|val;
  2101.         break;
  2102.     case 'g':
  2103.         the_ins.opcode[0]|=val=0xff;
  2104.         break;
  2105.     case 'i':
  2106.         the_ins.opcode[0]|=val<<9;
  2107.         break;
  2108.     case 'C':
  2109.         the_ins.opcode[1]|=val;
  2110.         break;
  2111.     case 'j':
  2112.         the_ins.opcode[1]|=val;
  2113.         the_ins.numo++;        /* What a hack */
  2114.         break;
  2115.     case 'k':
  2116.         the_ins.opcode[1]|=val<<4;
  2117.         break;
  2118.     case 'b':
  2119.     case 'w':
  2120.     case 'l':
  2121.         break;
  2122.     case 'c':
  2123.     default:
  2124.         abort();
  2125.     }
  2126. }
  2127.  
  2128. install_gen_operand(mode,val)
  2129. {
  2130.     switch(mode) {
  2131.     case 's':
  2132.         the_ins.opcode[0]|=val;
  2133.         break;
  2134.     case 'd':
  2135.             /* This is a kludge!!! */
  2136.         the_ins.opcode[0]|=(val&0x07)<<9|(val&0x38)<<3;
  2137.         break;
  2138.     case 'b':
  2139.     case 'w':
  2140.     case 'l':
  2141.     case 'f':
  2142.     case 'F':
  2143.     case 'x':
  2144.     case 'p':
  2145.         the_ins.opcode[0]|=val;
  2146.         break;
  2147.         /* more stuff goes here */
  2148.     default:
  2149.         abort();
  2150.     }
  2151. }
  2152.  
  2153. char *
  2154. crack_operand(str,opP)
  2155. register char *str;
  2156. register struct m68k_op *opP;
  2157. {
  2158.     register int parens;
  2159.     register int c;
  2160.     register char *beg_str;
  2161.  
  2162.     if(!str) {
  2163.         return str;
  2164.     }
  2165.     beg_str=str;
  2166.     for(parens=0;*str && (parens>0 || notend(str));str++) {
  2167.         if(*str=='(') parens++;
  2168.         else if(*str==')') {
  2169.             if(!parens) {        /* ERROR */
  2170.                 opP->error="Extra )";
  2171.                 return str;
  2172.             }
  2173.             --parens;
  2174.         }
  2175.     }
  2176.     if(!*str && parens) {        /* ERROR */
  2177.         opP->error="Missing )";
  2178.         return str;
  2179.     }
  2180.     c= *str;
  2181.     *str='\0';
  2182.     if(m68k_ip_op(beg_str,opP)==FAIL) {
  2183.         *str=c;
  2184.         return str;
  2185.     }
  2186.     *str=c;
  2187.     if(c=='}')
  2188.         c= *++str;        /* JF bitfield hack */
  2189.     if(c) {
  2190.          c= *++str;
  2191.         if(!c)
  2192.             as_warn("Missing operand");
  2193.     }
  2194.     return str;
  2195. }
  2196.  
  2197. /* See the comment up above where the #define notend(... is */
  2198. #if 0
  2199. notend(s)
  2200. char *s;
  2201. {
  2202.     if(*s==',') return 0;
  2203.     if(*s=='{' || *s=='}')
  2204.         return 0;
  2205.     if(*s!=':') return 1;
  2206.         /* This kludge here is for the division cmd, which is a kludge */
  2207.     if(index("aAdD#",s[1])) return 0;
  2208.     return 1;
  2209. }
  2210. #endif
  2211.  
  2212. /* This is the guts of the machine-dependent assembler.  STR points to a
  2213.    machine dependent instruction.  This funciton is supposed to emit
  2214.    the frags/bytes it assembles to.
  2215.  */
  2216. void
  2217. md_assemble(str)
  2218. char *str;
  2219. {
  2220.     char *er;
  2221.     short    *fromP;
  2222.     char    *toP;
  2223.     int    m,n;
  2224.     char    *to_beg_P;
  2225.  
  2226.     bzero((char *)(&the_ins),sizeof(the_ins));    /* JF for paranoia sake */
  2227.     m68_ip(str);
  2228.     er=the_ins.error;
  2229.     if(!er) {
  2230.         for(n=the_ins.numargs;n;--n)
  2231.             if(the_ins.operands[n].error) {
  2232.                 er=the_ins.operands[n].error;
  2233.                 break;
  2234.             }
  2235.     }
  2236.     if(er) {
  2237.         as_warn("\"%s\" -- Statement '%s' ignored",er,str);
  2238.         return;
  2239.     }
  2240.  
  2241.     if(the_ins.nfrag==0) {    /* No frag hacking involved; just put it out */
  2242.         toP=frag_more(2*the_ins.numo);
  2243.         fromP= &the_ins.opcode[0];
  2244.         for(m=the_ins.numo;m;--m) {
  2245.             md_number_to_chars(toP,(long)(*fromP),2);
  2246.             toP+=2;
  2247.             fromP++;
  2248.         }
  2249.             /* put out symbol-dependent info */
  2250.         for(m=0;m<the_ins.nrel;m++) {
  2251.             switch(the_ins.reloc[m].wid) {
  2252.             case 'B':
  2253.                 n=1;
  2254.                 break;
  2255.             case 'b':
  2256.                 n=1;
  2257.                 break;
  2258.             case 'w':
  2259.                 n=2;
  2260.                 break;
  2261.             case 'l':
  2262.                 n=4;
  2263.                 break;
  2264.             default:
  2265.                 as_fatal("confusing width %c",the_ins.reloc[m].wid);
  2266.             }
  2267.  
  2268.             fix_new(frag_now,
  2269.                 (toP-frag_now->fr_literal)-the_ins.numo*2+the_ins.reloc[m].n,
  2270.                 n,
  2271.                 the_ins.reloc[m].add,
  2272.                 the_ins.reloc[m].sub,
  2273.                 the_ins.reloc[m].off,
  2274.                 the_ins.reloc[m].pcrel);
  2275.         }
  2276.         return;
  2277.     }
  2278.  
  2279.         /* There's some frag hacking */
  2280.     for(n=0,fromP= &the_ins.opcode[0];n<the_ins.nfrag;n++) {
  2281.         int wid;
  2282.  
  2283.         if(n==0) wid=2*the_ins.fragb[n].fragoff;
  2284.         else wid=2*(the_ins.numo-the_ins.fragb[n-1].fragoff);
  2285.         toP=frag_more(wid);
  2286.         to_beg_P=toP;
  2287.         for(m=wid/2;m;--m) {
  2288.             md_number_to_chars(toP,(long)(*fromP),2);
  2289.             toP+=2;
  2290.             fromP++;
  2291.         }
  2292.         for(m=0;m<the_ins.nrel;m++) {
  2293.             if((the_ins.reloc[m].n)/2>the_ins.fragb[n].fragoff)
  2294.                 break;
  2295.             wid=the_ins.reloc[m].wid;
  2296.             if(wid==0)
  2297.                 continue;
  2298.             the_ins.reloc[m].wid=0;
  2299.             wid = (wid=='b') ? 1 : (wid=='w') ? 2 : (wid=='l') ? 4 : 4000;
  2300.  
  2301.             fix_new(frag_now,
  2302.                 (toP-frag_now->fr_literal)-the_ins.numo*2+the_ins.reloc[m].n,
  2303.                 wid,
  2304.                 the_ins.reloc[m].add,
  2305.                 the_ins.reloc[m].sub,
  2306.                 the_ins.reloc[m].off,
  2307.                 the_ins.reloc[m].pcrel);
  2308.         }
  2309.         know(the_ins.fragb[n].fadd);
  2310.         (void)frag_var(rs_machine_dependent,4,0,(relax_substateT)(the_ins.fragb[n].fragty),
  2311.  the_ins.fragb[n].fadd,the_ins.fragb[n].foff,to_beg_P);
  2312.     }
  2313.     n=(the_ins.numo-the_ins.fragb[n-1].fragoff);
  2314.     if(n) {
  2315.         toP=frag_more(n*sizeof(short));
  2316.         while(n--) {
  2317.             md_number_to_chars(toP,(long)(*fromP),2);
  2318.             toP+=2;
  2319.             fromP++;
  2320.         }
  2321.     }
  2322. }
  2323.  
  2324. /* This function is called once, at assembler startup time.  This should
  2325.    set up all the tables, etc that the MD part of the assembler needs
  2326.  */
  2327. void
  2328. md_begin()
  2329. {
  2330. /*
  2331.  * md_begin -- set up hash tables with 68000 instructions.
  2332.  * similar to what the vax assembler does.  ---phr
  2333.  */
  2334.     /* RMS claims the thing to do is take the m68k-opcode.h table, and make
  2335.        a copy of it at runtime, adding in the information we want but isn't
  2336.        there.  I think it'd be better to have an awk script hack the table
  2337.        at compile time.  Or even just xstr the table and use it as-is.  But
  2338.        my lord ghod hath spoken, so we do it this way.  Excuse the ugly var
  2339.        names.  */
  2340.  
  2341.     register struct m68k_opcode *ins;
  2342.     register struct m68_incant *hack,
  2343.         *slak;
  2344.     register char *retval = 0;        /* empty string, or error msg text */
  2345.     register int i;
  2346.     register char c;
  2347.  
  2348.     if ((op_hash = hash_new()) == NULL)
  2349.         as_fatal("Virtual memory exhausted");
  2350.  
  2351.     obstack_begin(&robyn,4000);
  2352.     for (ins = m68k_opcodes; ins < endop; ins++) {
  2353.         hack=slak=(struct m68_incant *)obstack_alloc(&robyn,sizeof(struct m68_incant));
  2354.         do {
  2355.             slak->m_operands=ins->args;
  2356.             slak->m_opnum=strlen(slak->m_operands)/2;
  2357.             slak->m_opcode=ins->opcode;
  2358.                 /* This is kludgey */
  2359.             slak->m_codenum=((ins->match)&0xffffL) ? 2 : 1;
  2360.             if((ins+1)!=endop && !strcmp(ins->name,(ins+1)->name)) {
  2361.                 slak->m_next=(struct m68_incant *)
  2362. obstack_alloc(&robyn,sizeof(struct m68_incant));
  2363.                 ins++;
  2364.             } else
  2365.                 slak->m_next=0;
  2366.             slak=slak->m_next;
  2367.         } while(slak);
  2368.         
  2369.         retval = hash_insert (op_hash, ins->name,(char *)hack);
  2370.             /* Didn't his mommy tell him about null pointers? */
  2371.         if(retval && *retval)
  2372.             as_fatal("Internal Error:  Can't hash %s: %s",ins->name,retval);
  2373.     }
  2374.  
  2375.     for (i = 0; i < sizeof(mklower_table) ; i++)
  2376.         mklower_table[i] = (isupper(c = (char) i)) ? tolower(c) : c;
  2377.  
  2378.     for (i = 0 ; i < sizeof(notend_table) ; i++) {
  2379.         notend_table[i] = 0;
  2380.         alt_notend_table[i] = 0;
  2381.     }
  2382.     notend_table[','] = 1;
  2383.     notend_table['{'] = 1;
  2384.     notend_table['}'] = 1;
  2385.     alt_notend_table['a'] = 1;
  2386.     alt_notend_table['A'] = 1;
  2387.     alt_notend_table['d'] = 1;
  2388.     alt_notend_table['D'] = 1;
  2389.     alt_notend_table['#'] = 1;
  2390. }
  2391.  
  2392. #if 0
  2393. #define notend(s) ((*s == ',' || *s == '}' || *s == '{' \
  2394.                    || (*s == ':' && index("aAdD#", s[1]))) \
  2395.                ? 0 : 1)
  2396. #endif
  2397.  
  2398. /* This funciton is called once, before the assembler exits.  It is
  2399.    supposed to do any final cleanup for this part of the assembler.
  2400.  */
  2401. void
  2402. md_end()
  2403. {
  2404. }
  2405.  
  2406. #define MAX_LITTLENUMS 6
  2407.  
  2408. /* Turn the string pointed to by litP into a floating point constant of type
  2409.    type, and emit the appropriate bytes.  The number of LITTLENUMS emitted
  2410.    is stored in *sizeP .  An error message is returned, or NULL on OK.
  2411.  */
  2412. char *
  2413. md_atof(type,litP,sizeP)
  2414. char type;
  2415. char *litP;
  2416. int *sizeP;
  2417. {
  2418.     int    prec;
  2419.     LITTLENUM_TYPE words[MAX_LITTLENUMS];
  2420.     LITTLENUM_TYPE *wordP;
  2421.     char    *t;
  2422.     char    *atof_m68k();
  2423.  
  2424.     switch(type) {
  2425.     case 'f':
  2426.     case 'F':
  2427.     case 's':
  2428.     case 'S':
  2429.         prec = 2;
  2430.         break;
  2431.  
  2432.     case 'd':
  2433.     case 'D':
  2434.     case 'r':
  2435.     case 'R':
  2436.         prec = 4;
  2437.         break;
  2438.  
  2439.     case 'x':
  2440.     case 'X':
  2441.         prec = 6;
  2442.         break;
  2443.  
  2444.     case 'p':
  2445.     case 'P':
  2446.         prec = 6;
  2447.         break;
  2448.  
  2449.     default:
  2450.         *sizeP=0;
  2451.         return "Bad call to MD_ATOF()";
  2452.     }
  2453.     t=atof_m68k(input_line_pointer,type,words);
  2454.     if(t)
  2455.         input_line_pointer=t;
  2456.  
  2457.     *sizeP=prec * sizeof(LITTLENUM_TYPE);
  2458.     for(wordP=words;prec--;) {
  2459.         md_number_to_chars(litP,(long)(*wordP++),sizeof(LITTLENUM_TYPE));
  2460.         litP+=sizeof(LITTLENUM_TYPE);
  2461.     }
  2462.     return "";    /* Someone should teach Dean about null pointers */
  2463. }
  2464.  
  2465. /* Turn an integer of n bytes (in val) into a stream of bytes appropriate
  2466.    for use in the a.out file, and stores them in the array pointed to by buf.
  2467.    This knows about the endian-ness of the target machine and does
  2468.    THE RIGHT THING, whatever it is.  Possible values for n are 1 (byte)
  2469.    2 (short) and 4 (long)  Floating numbers are put out as a series of
  2470.    LITTLENUMS (shorts, here at least)
  2471.  */
  2472. void
  2473. md_number_to_chars(buf,val,n)
  2474. char    *buf;
  2475. long    val;
  2476. {
  2477.     switch(n) {
  2478.     case 1:
  2479.         *buf++=val;
  2480.         break;
  2481.     case 2:
  2482.         *buf++=(val>>8);
  2483.         *buf++=val;
  2484.         break;
  2485.     case 4:
  2486.         *buf++=(val>>24);
  2487.         *buf++=(val>>16);
  2488.         *buf++=(val>>8);
  2489.         *buf++=val;
  2490.         break;
  2491.     default:
  2492.         abort();
  2493.     }
  2494. }
  2495.  
  2496. /* *fragP has been relaxed to its final size, and now needs to have
  2497.    the bytes inside it modified to conform to the new size  There is UGLY
  2498.    MAGIC here. ..
  2499.  */
  2500. void
  2501. md_convert_frag(fragP)
  2502. register fragS *fragP;
  2503. {
  2504.   long disp;
  2505.   long ext;
  2506.  
  2507.   /* Address in gas core of the place to store the displacement.  */
  2508.   register char *buffer_address = fragP -> fr_fix + fragP -> fr_literal;
  2509.   /* Address in object code of the displacement.  */
  2510.   register int object_address = fragP -> fr_fix + fragP -> fr_address;
  2511.  
  2512.   know(fragP->fr_symbol);
  2513.  
  2514.   /* The displacement of the address, from current location.  */
  2515.   disp = (fragP->fr_symbol->sy_value + fragP->fr_offset) - object_address;
  2516.  
  2517.   switch(fragP->fr_subtype) {
  2518.   case TAB(BRANCH,BYTE):
  2519.     know(issbyte(disp));
  2520.     if(disp==0)
  2521.       as_warn("short branch with zero offset: use :w");
  2522.     fragP->fr_opcode[1]=disp;
  2523.     ext=0;
  2524.     break;
  2525.   case TAB(BRANCH,SHORT):
  2526.     know(issword(disp));
  2527.     fragP->fr_opcode[1]=0x00;
  2528.     ext=2;
  2529.     break;
  2530.   case TAB(BRANCH,LONG):
  2531.     fragP->fr_opcode[1]=0xff;
  2532.     ext=4;
  2533.     break;
  2534.   case TAB(FBRANCH,SHORT):
  2535.     know((fragP->fr_opcode[1]&0x40)==0);
  2536.     ext=2;
  2537.     break;
  2538.   case TAB(FBRANCH,LONG):
  2539.     fragP->fr_opcode[1]|=0x40;    /* Turn on LONG bit */
  2540.     ext=4;
  2541.     break;
  2542.   case TAB(PCREL,SHORT):
  2543.     ext=2;
  2544.     break;
  2545.   case TAB(PCREL,LONG):
  2546.     /* The thing to do here is force it to ABSOLUTE LONG, since
  2547.        PCREL is really trying to shorten an ABSOLUTE address anyway */
  2548.     /* JF FOO This code has not been tested */
  2549.     subseg_change(SEG_TEXT,0);
  2550.     fix_new(fragP, fragP->fr_fix, 4, fragP->fr_symbol, 0, fragP->fr_offset, 0);
  2551.     if((fragP->fr_opcode[1] & 0x3F) != 0x3A)
  2552.         as_warn("Internal error (long PC-relative operand) for insn 0x%04lx at 0x%lx",
  2553.             fragP->fr_opcode[0],fragP->fr_address);
  2554.     fragP->fr_opcode[1]&= ~0x3F;
  2555.     fragP->fr_opcode[1]|=0x39;    /* Mode 7.1 */
  2556.     fragP->fr_fix+=4;
  2557.     /* md_number_to_chars(buffer_address,
  2558.                (long)(fragP->fr_symbol->sy_value + fragP->fr_offset),
  2559.                4); */
  2560.     ext=0;
  2561.   }
  2562.   if(ext) {
  2563.     md_number_to_chars(buffer_address,(long)disp,(int)ext);
  2564.     fragP->fr_fix+=ext;
  2565.   }
  2566. }
  2567.  
  2568. /* Force truly undefined symbols to their maximum size, and generally set up
  2569.    the frag list to be relaxed
  2570.  */
  2571. md_estimate_size_before_relax(fragP,segtype)
  2572. register fragS *fragP;
  2573. {
  2574.     int    old_fix;
  2575.  
  2576.     old_fix=fragP->fr_fix;
  2577.  
  2578.     switch(fragP->fr_subtype) {
  2579.     case TAB(BRANCH,SZ_UNDEF):
  2580.         if((fragP->fr_symbol->sy_type&N_TYPE)==segtype) {
  2581.             fragP->fr_subtype=TAB(BRANCH,BYTE);
  2582.             /* Don't break, fall through into BYTE case */
  2583.         } else {
  2584.             /* Symbol is still undefined.  Make it simple */
  2585.             fix_new(fragP,(int)(fragP->fr_fix),4,fragP->fr_symbol,
  2586.  (symbolS *)0,fragP->fr_offset+4,1);
  2587.             fragP->fr_fix+=4;
  2588.             fragP->fr_opcode[1]=0xff;
  2589.             frag_wane(fragP);
  2590.             break;
  2591.         }
  2592.     case TAB(BRANCH,BYTE):
  2593.             /* We can't do a short jump to the next instruction,
  2594.                so we force word mode.  */
  2595.         if(fragP->fr_symbol->sy_value==0 &&
  2596.  fragP->fr_symbol->sy_frag==fragP->fr_next) {
  2597.             fragP->fr_subtype=TAB(BRANCH,SHORT);
  2598.             fragP->fr_var+=2;
  2599.         }
  2600.         break;
  2601.     case TAB(FBRANCH,SZ_UNDEF):
  2602.         if((fragP->fr_symbol->sy_type&N_TYPE)==segtype) {
  2603.             fragP->fr_subtype=TAB(FBRANCH,SHORT);
  2604.             fragP->fr_var+=2;
  2605.         } else {
  2606.             fragP->fr_subtype=TAB(FBRANCH,LONG);
  2607.             fragP->fr_var+=4;
  2608.         }
  2609.         break;
  2610.     case TAB(PCREL,SZ_UNDEF):
  2611.         if((fragP->fr_symbol->sy_type&N_TYPE)==segtype) {
  2612.             fragP->fr_subtype=TAB(PCREL,SHORT);
  2613.             fragP->fr_var+=2;
  2614.         } else {
  2615.             fragP->fr_subtype=TAB(PCREL,LONG);
  2616.             fragP->fr_var+=4;
  2617.         }
  2618.         break;
  2619.     default:
  2620.         break;
  2621.     }
  2622.     return fragP->fr_var + fragP->fr_fix - old_fix;
  2623. }
  2624.  
  2625.  
  2626. /* Different values of OK tell what its OK to return.  Things that aren't OK are an error (what a shock, no?)
  2627.  
  2628.     0:  Everything is OK
  2629.     10:  Absolute 1:8    only
  2630.     20:  Absolute 0:7    only
  2631.     30:  absolute 0:15    only
  2632.     40:  Absolute 0:31    only
  2633.     50:  absolute 0:127    only
  2634.     55:  absolute -64:63    only
  2635.     60:  absolute -128:127    only
  2636.     70:  absolute 0:4095    only
  2637.     80:  No bignums
  2638.  
  2639. */
  2640. get_num(exp,ok)
  2641. struct m68k_exp *exp;
  2642. {
  2643. #ifdef TEST2
  2644.     long    l = 0;
  2645.  
  2646.     if(!exp->e_beg)
  2647.         return 0;
  2648.     if(*exp->e_beg=='0') {
  2649.         if(exp->e_beg[1]=='x')
  2650.             sscanf(exp->e_beg+2,"%x",&l);
  2651.         else
  2652.             sscanf(exp->e_beg+1,"%O",&l);
  2653.         return l;
  2654.     }
  2655.     return atol(exp->e_beg);
  2656. #else
  2657.     char    *save_in;
  2658.     char    c_save;
  2659.  
  2660.     if(!exp) {
  2661.         /* Can't do anything */
  2662.         return 0;
  2663.     }
  2664.     if(!exp->e_beg || !exp->e_end) {
  2665.         seg(exp)=SEG_ABSOLUTE;
  2666.         adds(exp)=0;
  2667.         subs(exp)=0;
  2668.         offs(exp)= (ok==10) ? 1 : 0;
  2669.         as_warn("Null expression defaults to %ld",offs(exp));
  2670.         return 0;
  2671.     }
  2672.  
  2673.     exp->e_siz=0;
  2674.     if(/* ok!=80 && */exp->e_end[-1]==':' && (exp->e_end-exp->e_beg)>2) {
  2675.         switch(exp->e_end[0]) {
  2676.         case 's':
  2677.         case 'b':
  2678.             exp->e_siz=1;
  2679.             break;
  2680.         case 'w':
  2681.             exp->e_siz=2;
  2682.             break;
  2683.         case 'l':
  2684.             exp->e_siz=3;
  2685.             break;
  2686.         default:
  2687.             as_warn("Unknown size for expression \"%c\"",exp->e_end[0]);
  2688.         }
  2689.         exp->e_end-=2;
  2690.     }
  2691.     c_save=exp->e_end[1];
  2692.     exp->e_end[1]='\0';
  2693.     save_in=input_line_pointer;
  2694.     input_line_pointer=exp->e_beg;
  2695.     switch(expression(&(exp->e_exp))) {
  2696.     case SEG_NONE:
  2697.         /* Do the same thing the VAX asm does */
  2698.         seg(exp)=SEG_ABSOLUTE;
  2699.         adds(exp)=0;
  2700.         subs(exp)=0;
  2701.         offs(exp)=0;
  2702.         if(ok==10) {
  2703.             as_warn("expression out of range: defaulting to 1");
  2704.             offs(exp)=1;
  2705.         }
  2706.         break;
  2707.     case SEG_ABSOLUTE:
  2708.         switch(ok) {
  2709.         case 10:
  2710.             if(offs(exp)<1 || offs(exp)>8) {
  2711.                 as_warn("expression out of range: defaulting to 1");
  2712.                 offs(exp)=1;
  2713.             }
  2714.             break;
  2715.         case 20:
  2716.             if(offs(exp)<0 || offs(exp)>7)
  2717.                 goto outrange;
  2718.             break;
  2719.         case 30:
  2720.             if(offs(exp)<0 || offs(exp)>15)
  2721.                 goto outrange;
  2722.             break;
  2723.         case 40:
  2724.             if(offs(exp)<0 || offs(exp)>31)
  2725.                 goto outrange;
  2726.             break;
  2727.         case 50:
  2728.             if(offs(exp)<0 || offs(exp)>127)
  2729.                 goto outrange;
  2730.             break;
  2731.         case 55:
  2732.             if(offs(exp)<-64 || offs(exp)>63)
  2733.                 goto outrange;
  2734.             break;
  2735.         case 60:
  2736.             if(offs(exp)<-128 || offs(exp)>127)
  2737.                 goto outrange;
  2738.             break;
  2739.         case 70:
  2740.             if(offs(exp)<0 || offs(exp)>4095) {
  2741.             outrange:
  2742.                 as_warn("expression out of range: defaulting to 0");
  2743.                 offs(exp)=0;
  2744.             }
  2745.             break;
  2746.         default:
  2747.             break;
  2748.         }
  2749.         break;
  2750.     case SEG_TEXT:
  2751.     case SEG_DATA:
  2752.     case SEG_BSS:
  2753.     case SEG_UNKNOWN:
  2754.     case SEG_DIFFERENCE:
  2755.         if(ok>=10 && ok<=70) {
  2756.             seg(exp)=SEG_ABSOLUTE;
  2757.             adds(exp)=0;
  2758.             subs(exp)=0;
  2759.             offs(exp)= (ok==10) ? 1 : 0;
  2760.             as_warn("Can't deal with expression \"%s\": defaulting to %ld",exp->e_beg,offs(exp));
  2761.         }
  2762.         break;
  2763.     case SEG_BIG:
  2764.         if(ok==80 && offs(exp)<0) {    /* HACK! Turn it into a long */
  2765.             LITTLENUM_TYPE words[6];
  2766.  
  2767.             gen_to_words(words,2,8L);/* These numbers are magic! */
  2768.             seg(exp)=SEG_ABSOLUTE;
  2769.             adds(exp)=0;
  2770.             subs(exp)=0;
  2771.             offs(exp)=words[1]|(words[0]<<16);
  2772.         } else if(ok!=0) {
  2773.             seg(exp)=SEG_ABSOLUTE;
  2774.             adds(exp)=0;
  2775.             subs(exp)=0;
  2776.             offs(exp)= (ok==10) ? 1 : 0;
  2777.             as_warn("Can't deal with expression \"%s\": defaulting to %ld",exp->e_beg,offs(exp));
  2778.         }
  2779.         break;
  2780.     default:
  2781.         abort();
  2782.     }
  2783.     if(input_line_pointer!=exp->e_end+1)
  2784.         as_warn("Ignoring junk after expression");
  2785.     exp->e_end[1]=c_save;
  2786.     input_line_pointer=save_in;
  2787.     if(exp->e_siz) {
  2788.         switch(exp->e_siz) {
  2789.         case 1:
  2790.             if(!isbyte(offs(exp)))
  2791.                 as_warn("expression doesn't fit in BYTE");
  2792.             break;
  2793.         case 2:
  2794.             if(!isword(offs(exp)))
  2795.                 as_warn("expression doesn't fit in WORD");
  2796.             break;
  2797.         }
  2798.     }
  2799.     return offs(exp);
  2800. #endif
  2801. }
  2802.  
  2803. /* These are the back-ends for the various machine dependent pseudo-ops.  */
  2804. void demand_empty_rest_of_line();    /* Hate those extra verbose names */
  2805.  
  2806. void
  2807. s_data1()
  2808. {
  2809.     subseg_new(SEG_DATA,1);
  2810.     demand_empty_rest_of_line();
  2811. }
  2812.  
  2813. void
  2814. s_data2()
  2815. {
  2816.     subseg_new(SEG_DATA,2);
  2817.     demand_empty_rest_of_line();
  2818. }
  2819.  
  2820. void
  2821. s_even()
  2822. {
  2823.     register int temp;
  2824.     register long int temp_fill;
  2825.  
  2826.     temp = 1;        /* JF should be 2? */
  2827.     temp_fill = get_absolute_expression ();
  2828.     if ( ! need_pass_2 ) /* Never make frag if expect extra pass. */
  2829.         frag_align (temp, (int)temp_fill);
  2830.     demand_empty_rest_of_line();
  2831. }
  2832.  
  2833. /* s_space is defined in read.c .skip is simply an alias to it. */
  2834.  
  2835. #ifdef TEST2
  2836.  
  2837. /* TEST2:  Test md_assemble() */
  2838. /* Warning, this routine probably doesn't work anymore */
  2839.  
  2840. main()
  2841. {
  2842.     struct m68_it the_ins;
  2843.     char buf[120];
  2844.     char *cp;
  2845.     int    n;
  2846.  
  2847.     m68_ip_begin();
  2848.     for(;;) {
  2849.         if(!gets(buf) || !*buf)
  2850.             break;
  2851.         if(buf[0]=='|' || buf[1]=='.')
  2852.             continue;
  2853.         for(cp=buf;*cp;cp++)
  2854.             if(*cp=='\t')
  2855.                 *cp=' ';
  2856.         if(is_label(buf))
  2857.             continue;
  2858.         bzero(&the_ins,sizeof(the_ins));
  2859.         m68_ip(&the_ins,buf);
  2860.         if(the_ins.error) {
  2861.             printf("Error %s in %s\n",the_ins.error,buf);
  2862.         } else {
  2863.             printf("Opcode(%d.%s): ",the_ins.numo,the_ins.args);
  2864.             for(n=0;n<the_ins.numo;n++)
  2865.                 printf(" 0x%x",the_ins.opcode[n]&0xffff);
  2866.             printf("    ");
  2867.             print_the_insn(&the_ins.opcode[0],stdout);
  2868.             (void)putchar('\n');
  2869.         }
  2870.         for(n=0;n<strlen(the_ins.args)/2;n++) {
  2871.             if(the_ins.operands[n].error) {
  2872.                 printf("op%d Error %s in %s\n",n,the_ins.operands[n].error,buf);
  2873.                 continue;
  2874.             }
  2875.             printf("mode %d, reg %d, ",the_ins.operands[n].mode,the_ins.operands[n].reg);
  2876.             if(the_ins.operands[n].b_const)
  2877.                 printf("Constant: '%.*s', ",1+the_ins.operands[n].e_const-the_ins.operands[n].b_const,the_ins.operands[n].b_const);
  2878.             printf("ireg %d, isiz %d, imul %d, ",the_ins.operands[n].ireg,the_ins.operands[n].isiz,the_ins.operands[n].imul);
  2879.             if(the_ins.operands[n].b_iadd)
  2880.                 printf("Iadd: '%.*s',",1+the_ins.operands[n].e_iadd-the_ins.operands[n].b_iadd,the_ins.operands[n].b_iadd);
  2881.             (void)putchar('\n');
  2882.         }
  2883.     }
  2884.     m68_ip_end();
  2885.     return 0;
  2886. }
  2887.  
  2888. is_label(str)
  2889. char *str;
  2890. {
  2891.     while(*str==' ')
  2892.         str++;
  2893.     while(*str && *str!=' ')
  2894.         str++;
  2895.     if(str[-1]==':' || str[1]=='=')
  2896.         return 1;
  2897.     return 0;
  2898. }
  2899.  
  2900. print_address(add,fp)
  2901. FILE *fp;
  2902. {
  2903.     fprintf(fp,"%#X",add);
  2904. }
  2905.  
  2906. #endif
  2907.  
  2908. /* Possible states for relaxation:
  2909.  
  2910. 0 0    branch offset    byte    (bra, etc)
  2911. 0 1            word
  2912. 0 2            long
  2913.  
  2914. 1 0    indexed offsets    byte    a0@(32,d4:w:1) etc
  2915. 1 1            word
  2916. 1 2            long
  2917.  
  2918. 2 0    two-offset index word-word a0@(32,d4)@(45) etc
  2919. 2 1            word-long
  2920. 2 2            long-word
  2921. 2 3            long-long
  2922.  
  2923. */
  2924.  
  2925.  
  2926.  
  2927. #ifdef DONTDEF
  2928. abort()
  2929. {
  2930.     printf("ABORT!\n");
  2931.     exit(12);
  2932. }
  2933.  
  2934. char *index(s,c)
  2935. char *s;
  2936. {
  2937.     while(*s!=c) {
  2938.         if(!*s) return 0;
  2939.         s++;
  2940.     }
  2941.     return s;
  2942. }
  2943.  
  2944. bzero(s,n)
  2945. char *s;
  2946. {
  2947.     while(n--)
  2948.         *s++=0;
  2949. }
  2950.  
  2951. print_frags()
  2952. {
  2953.     fragS *fragP;
  2954.     extern fragS *text_frag_root;
  2955.  
  2956.     for(fragP=text_frag_root;fragP;fragP=fragP->fr_next) {
  2957.         printf("addr %lu  next 0x%x  fix %ld  var %ld  symbol 0x%x  offset %ld\n",
  2958.  fragP->fr_address,fragP->fr_next,fragP->fr_fix,fragP->fr_var,fragP->fr_symbol,fragP->fr_offset);
  2959.         printf("opcode 0x%x  type %d  subtype %d\n\n",fragP->fr_opcode,fragP->fr_type,fragP->fr_subtype);
  2960.     }
  2961.     fflush(stdout);
  2962.     return 0;
  2963. }
  2964. #endif
  2965.  
  2966. #ifdef DONTDEF
  2967. /*VARARGS1*/
  2968. panic(format,args)
  2969. char *format;
  2970. {
  2971.     fputs("Internal error:",stderr);
  2972.     _doprnt(format,&args,stderr);
  2973.     (void)putc('\n',stderr);
  2974.     as_where();
  2975.     abort();
  2976. }
  2977. #endif
  2978.